Skip to content

Commit 58af49a

Browse files
authored
Enable nullable in XmlPath (#872)
1 parent 3427fc9 commit 58af49a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/DocumentFormat.OpenXml/XmlPath.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#nullable disable
5-
64
using DocumentFormat.OpenXml.Framework;
75
using DocumentFormat.OpenXml.Packaging;
86
using System;
97
using System.Collections.Generic;
108
using System.ComponentModel;
119
using System.Diagnostics;
10+
using System.Diagnostics.CodeAnalysis;
1211
using System.Linq;
1312
using System.Text;
1413
using System.Xml;
@@ -32,7 +31,6 @@ internal XmlPath(OpenXmlElement element)
3231
}
3332

3433
PartUri = element.GetPartUri();
35-
3634
XPath = TryBuildXPath(GetElements(element), out var namespaces);
3735

3836
#pragma warning disable CS0618 // Type or member is obsolete
@@ -64,6 +62,12 @@ internal XmlPath(OpenXmlPart part)
6462
}
6563

6664
PartUri = part.Uri;
65+
XPath = string.Empty;
66+
67+
#pragma warning disable CS0618 // Type or member is obsolete
68+
Namespaces = ReadOnlyWrapper.Instance;
69+
NamespacesDefinitions = Cached.Array<string>();
70+
#pragma warning restore CS0618 // Type or member is obsolete
6771
}
6872

6973
/// <summary>
@@ -93,7 +97,7 @@ internal XmlPath(OpenXmlPart part)
9397
/// </summary>
9498
/// <param name="element">The OpenXmlElement.</param>
9599
/// <returns>XmlPath to this element from root element.</returns>
96-
internal static XmlPath GetXPath(OpenXmlElement element)
100+
internal static XmlPath? GetXPath(OpenXmlElement? element)
97101
{
98102
if (element is null)
99103
{
@@ -103,7 +107,7 @@ internal static XmlPath GetXPath(OpenXmlElement element)
103107
return new XmlPath(element);
104108
}
105109

106-
internal static XmlPath GetXPath(OpenXmlPart part)
110+
internal static XmlPath? GetXPath(OpenXmlPart? part)
107111
{
108112
if (part is null)
109113
{
@@ -113,7 +117,7 @@ internal static XmlPath GetXPath(OpenXmlPart part)
113117
return new XmlPath(part);
114118
}
115119

116-
private static string TryBuildXPath(Stack<OpenXmlElement> elements, out XmlNamespaceManager namespaces)
120+
private static string TryBuildXPath(Stack<OpenXmlElement> elements, [MaybeNullWhen(false)] out XmlNamespaceManager namespaces)
117121
{
118122
if (elements.Count == 0)
119123
{
@@ -163,7 +167,7 @@ private static string TryBuildXPath(Stack<OpenXmlElement> elements, out XmlNames
163167
return xpath.ToString();
164168
}
165169

166-
private static Stack<OpenXmlElement> GetElements(OpenXmlElement element)
170+
private static Stack<OpenXmlElement> GetElements(OpenXmlElement? element)
167171
{
168172
var elements = new Stack<OpenXmlElement>();
169173

@@ -189,9 +193,9 @@ public ReadOnlyWrapper(IXmlNamespaceResolver other)
189193

190194
public IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scope) => _other.GetNamespacesInScope(scope);
191195

192-
public string LookupNamespace(string prefix) => _other.LookupNamespace(prefix);
196+
public string? LookupNamespace(string prefix) => _other.LookupNamespace(prefix);
193197

194-
public string LookupPrefix(string namespaceName) => _other.LookupPrefix(namespaceName);
198+
public string? LookupPrefix(string namespaceName) => _other.LookupPrefix(namespaceName);
195199
}
196200
}
197201
}

0 commit comments

Comments
 (0)