XmlNodeReader.NamespaceURI Property

C#
public string NamespaceURI { get; }
Visual Basic
Public ReadOnly Property NamespaceURI As String
C++
public:
    property System::String ^ NamespaceURI {
        System::String ^ get();
    }
        
F#
member NamespaceURI : string with get

Description

Gets the Namespace URI (as defined in the World Wide Web Consortium (W3C) Namespaces in XML specification) of the current node.

Property Value

A string representing the Namespace URI of the current node.

Remarks

This property returns an empty string for nodes that do not have a namespace, such as attribute nodes that are not in a namespace.

Important

For element and attribute nodes, this property returns the URI of the namespace the node belongs to. For attribute nodes that are not in a namespace, it returns an empty string.

Example

C# Example

The following example demonstrates how to use the NamespaceURI property with an XmlNodeReader.

using System;
using System.Xml;

public class Sample
{
    public static void Main(string[] args)
    {
        string xmlString = @"<root xmlns='http://www.example.com/ns1' xmlns:p='http://www.example.com/ns2'>
                                <child p:attr='value1'/>
                                <anotherChild>Text</anotherChild>
                             </root>";

        using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlString)))
        {
            while (reader.Read())
            {
                Console.WriteLine($"NodeType: {reader.NodeType}");
                Console.WriteLine($"Name: {reader.Name}");
                Console.WriteLine($"NamespaceURI: '{reader.NamespaceURI}'");
                Console.WriteLine("--------------------");
            }
        }
    }
}
            

Output:

NodeType: Element
Name: root
NamespaceURI: 'http://www.example.com/ns1'
--------------------
NodeType: Element
Name: p:attr
NamespaceURI: 'http://www.example.com/ns2'
--------------------
NodeType: Text
Name: #text
NamespaceURI: ''
--------------------
NodeType: Element
Name: anotherChild
NamespaceURI: 'http://www.example.com/ns1'
--------------------
NodeType: Text
Name: #text
NamespaceURI: ''
--------------------
NodeType: EndElement
Name: anotherChild
NamespaceURI: 'http://www.example.com/ns1'
--------------------
NodeType: EndElement
Name: root
NamespaceURI: 'http://www.example.com/ns1'
--------------------
            

See Also