Table of Contents

XmlNamespaceResolver Class

Definition

Namespace
Bodu.Xml.Linq
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.3.0
Source
XmlNamespaceResolver.cs

Resolves child XElement and XName instances against the default XML namespace of a supplied root element, removing the need to qualify every lookup with an inline namespace prefix.

public sealed class XmlNamespaceResolver
Inheritance
XmlNamespaceResolver
Inherited Members
Extension Methods

Examples

XDocument doc = XDocument.Parse(
    "<library xmlns='urn:books'><book><title>Foundation</title></book></library>");

var ns = new XmlNamespaceResolver(doc.Root!);

// Look up by local name without re-stating the namespace at every call site.
XElement? book  = ns.Element(doc.Root!, "book");
XElement? title = ns.Element(book!,     "title");
Console.WriteLine(title?.Value); // Foundation

Remarks

Working with System.Xml.Linq documents that carry a default namespace requires every element access to be qualified with that namespace; otherwise Element(XName) returns null even when the named element is present. Unprefixed attributes are different: they belong to no namespace even under a default namespace, so look them up by plain local name rather than through Name(string). XmlNamespaceResolver captures the namespace once from the document's root element and exposes Element(XElement, string), Elements(XElement, string), and Name(string) helpers that pre-qualify lookups, allowing call-sites to reference elements by local name only.

The captured namespace is fixed for the lifetime of the instance - it is not refreshed if the underlying document mutates, and the resolver does not retain a reference to the root element itself. Callers that work with multiple namespaces in the same document should construct a separate resolver per namespace.

An element without an explicit namespace carries None (the empty namespace) - never null - so the resolver simply captures whatever namespace the root exposes, including the empty one. Lookups then resolve against that captured namespace.

Constructors

XmlNamespaceResolver(XElement)

Initializes a new instance of the XmlNamespaceResolver class with the specified root element.

public XmlNamespaceResolver(XElement root)

Parameters

root XElement

The root element from which to extract the default namespace. Must not be null.

Remarks

Namespace is never null - an element without an explicit namespace reports None - so the captured namespace may be the empty namespace but the construction itself cannot fail for a non-null root.

Exceptions

ArgumentNullException

Thrown if root is null.

Methods

Element(XElement, string)

Safely gets a child element with the specified local name in the current namespace.

public XElement? Element(XElement parent, string localName)

Parameters

parent XElement

The parent element.

localName string

The local name of the child element.

Returns

XElement

The matching child XElement, or null if not found.

Exceptions

ArgumentNullException

Thrown when parent or localName is null.

ArgumentException

Thrown when localName is empty.

XmlException

Thrown when localName is not a valid XML local name.

Elements(XElement, string)

Safely gets all child elements with the specified local name in the current namespace.

public IEnumerable<XElement> Elements(XElement parent, string localName)

Parameters

parent XElement

The parent element.

localName string

The local name of the child elements.

Returns

IEnumerable<XElement>

An enumerable of matching XElement objects.

Exceptions

ArgumentNullException

Thrown when parent or localName is null.

ArgumentException

Thrown when localName is empty.

XmlException

Thrown when localName is not a valid XML local name.

Name(string)

Gets the fully qualified XName for the given local name in the current namespace.

public XName Name(string localName)

Parameters

localName string

The local (unqualified) element name.

Returns

XName

The namespaced XName.

Remarks

Use the result for element names, and for attribute names only where the attribute is explicitly qualified with the captured namespace; an unprefixed attribute belongs to no namespace.

Exceptions

ArgumentNullException

Thrown when localName is null.

ArgumentException

Thrown when localName is empty.

XmlException

Thrown when localName is not a valid XML local name.

Applies to

ProductVersions
.NET8, 10