Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XNamespace Class

 

Represents an XML namespace. This class cannot be inherited.

Namespace:   System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

System.Object
  System.Xml.Linq.XNamespace

public sealed class XNamespace

NameDescription
System_CAPS_pubpropertyNamespaceName

Gets the Uniform Resource Identifier (URI) of this namespace.

System_CAPS_pubpropertySystem_CAPS_staticNone

Gets the XNamespace object that corresponds to no namespace.

System_CAPS_pubpropertySystem_CAPS_staticXml

Gets the XNamespace object that corresponds to the XML URI (http://www.w3.org/XML/1998/namespace).

System_CAPS_pubpropertySystem_CAPS_staticXmlns

Gets the XNamespace object that corresponds to the xmlns URI (http://www.w3.org/2000/xmlns/).

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified XNamespace is equal to the current XNamespace.(Overrides Object.Equals(Object).)

System_CAPS_pubmethodSystem_CAPS_staticGet(String)

Gets an XNamespace for the specified Uniform Resource Identifier (URI).

System_CAPS_pubmethodGetHashCode()

Gets a hash code for this XNamespace.(Overrides Object.GetHashCode().)

System_CAPS_pubmethodGetName(String)

Returns an XName object created from this XNamespace and the specified local name.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns the URI of this XNamespace.(Overrides Object.ToString().)

NameDescription
System_CAPS_puboperatorSystem_CAPS_staticAddition(XNamespace, String)

Combines an XNamespace object with a local name to create an XName.

System_CAPS_puboperatorSystem_CAPS_staticEquality(XNamespace, XNamespace)

Returns a value indicating whether two instances of XNamespace are equal.

System_CAPS_puboperatorSystem_CAPS_staticImplicit(String to XNamespace)

Converts a string containing a Uniform Resource Identifier (URI) to an XNamespace.

System_CAPS_puboperatorSystem_CAPS_staticInequality(XNamespace, XNamespace)

Returns a value indicating whether two instances of XNamespace are not equal.

This class represents the XML construct of namespaces.

Every XName contains an XNamespace. Even if an element is not in a namespace, the element's XName still contains a namespace, XNamespace.None. The XName.Namespace property is guaranteed to not be null.

The most common way to create an XNamespace object is to simply assign a string to it. You can then combine the namespace with a local name by using the override of the addition operator. The following example shows this idiom:


                XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root", "Content");
Console.WriteLine(root);

However, in Visual Basic, you would typically declare a global default namespace, as follows:


                Imports <xmlns='http://www.adventure-works.com'>

Module Module1
    Sub Main()
        Dim root As XElement = _
            <Root>Content</Root>
        Console.WriteLine(root)
    End Sub
End Module

This example produces the following output:


<Root xmlns="http://www.adventure-works.com">Content</Root>

Assigning a string to an XNamespace uses the implicit conversion from String.

See How to: Create a Document with Namespaces (C#) (LINQ to XML) for more information and examples.

See Namespaces in Visual Basic (LINQ to XML) for more information on using namespaces in Visual Basic.

If you create an attribute that declares a namespace, the prefix specified in the attribute will be persisted in the serialized XML. To create an attribute that declares a namespace with a prefix, you create an attribute where the namespace of the name of the attribute is Xmlns, and the name of the attribute is the namespace prefix. The value of the attribute is the URI of the namespace. The following example shows this idiom:


                XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
    "Content");
Console.WriteLine(root);

In Visual Basic, instead of creating a namespace node to control namespace prefixes, you would typically use a global namespace declaration:


                Imports <xmlns:aw='http://www.adventure-works.com'>

Module Module1
    Sub Main()
        Dim root As XElement = _
            <aw:Root>Content</aw:Root>
        Console.WriteLine(root)
    End Sub
End Module

This example produces the following output:

<aw:Root xmlns:aw="http://www.adventure-works.com">Content</aw:Root>

For more information, see How to: Control Namespace Prefixes (C#) (LINQ to XML).

When constructing an attribute that will be a namespace, if the attribute name has the special value of "xmlns", then when the XML tree is serialized, the namespace will be declared as the default namespace. The special attribute with the name of "xmlns" itself is not in any namespace. The value of the attribute is the namespace URI.

The following example creates an XML tree that contains an attribute that is declared in such a way that the namespace will become the default namespace:


                XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute("xmlns", "http://www.adventure-works.com"),
    new XElement(aw + "Child", "content")
);
Console.WriteLine(root);

In Visual Basic, instead of creating a namespace node to create a default namespace, you would typically use a global default namespace declaration:


                Imports <xmlns='http://www.adventure-works.com'>

Module Module1
    Sub Main()
        Dim root As XElement = _
            <Root>
                <Child>content</Child>
            </Root>
        Console.WriteLine(root)
    End Sub
End Module

This example produces the following output:

<Root xmlns="http://www.adventure-works.com">
  <Child>content</Child>
</Root>

XNamespace objects are guaranteed to be atomized; that is, if two XNamespace objects have exactly the same URI, they will share the same instance. The equality and comparison operators are provided explicitly for this purpose.

Another way to specify a namespace and a local name is to use an expanded name in the form {namespace}name:

[C#]

XElement e = new XElement("{http://www.adventure-works.com}Root",
     new XAttribute("{http://www.adventure-works.com}Att", "content")
);
Console.WriteLine(e);

                Dim e As XElement = New XElement("{http://www.adventure-works.com}Root", _
     New XAttribute("{http://www.adventure-works.com}Att", "content") _
)
Console.WriteLine(e)

This example produces the following output:

<Root p1:Att="content" xmlns:p1="http://www.adventure-works.com" xmlns="http://www.adventure-works.com" />

This approach has performance implications. Each time that you pass a string that contains an expanded name to LINQ to XML, it must parse the name, find the atomized namespace, and find the atomized name. This process takes CPU time. If performance is important, you may want to use a different approach.

With Visual Basic, the recommended approach is to use XML literals, which does not involve the use of expanded names.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft