Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XNamespace Class

Represents an XML namespace. This class cannot be inherited.

System.Object
  System.Xml.Linq.XNamespace

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)
public sealed class XNamespace

The XNamespace type exposes the following members.

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NamespaceNameGets the Uniform Resource Identifier (URI) of this namespace.
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360NoneGets the XNamespace object that corresponds to no namespace.
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360XmlGets the XNamespace object that corresponds to the XML URI (http://www.w3.org/XML/1998/namespace).
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360XmlnsGets the XNamespace object that corresponds to the xmlns URI (http://www.w3.org/2000/xmlns/).
Top
  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360EqualsDetermines whether the specified XNamespace is equal to the current XNamespace. (Overrides Object.Equals(Object).)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetGets an XNamespace for the specified Uniform Resource Identifier (URI).
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeGets a hash code for this XNamespace. (Overrides Object.GetHashCode().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetNameReturns an XName object created from this XNamespace and the specified local name.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns the URI of this XNamespace. (Overrides Object.ToString().)
Top
  NameDescription
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360AdditionCombines an XNamespace object with a local name to create an XName.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360EqualityReturns a value indicating whether two instances of XNamespace are equal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360Implicit(String to XNamespace)Converts a string containing a Uniform Resource Identifier (URI) to an XNamespace.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360InequalityReturns a value indicating whether two instances of XNamespace are not equal.
Top

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.

Creating an XNamespace Object

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:


StringBuilder output = new StringBuilder();
XNamespace aw = "http://http://www.adventure-works.com";
XElement root = new XElement(aw + "Root", "Content");
output.Append(root + Environment.NewLine);

OutputTextBlock.Text = output.ToString();


Controlling Namespace Prefixes

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:


StringBuilder output = new StringBuilder();
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
    "Content");
output.Append(root + Environment.NewLine);

OutputTextBlock.Text = output.ToString();


Creating a Default Namespace

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:


StringBuilder output = new StringBuilder();
XNamespace aw = "http://http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XAttribute("xmlns", "http://http://www.adventure-works.com"),
    new XElement(aw + "Child", "content")
);
output.Append(root + Environment.NewLine);

OutputTextBlock.Text = output.ToString();


XNamespace Atomization

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.

Using Expanded Names

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")
);
Dim e As XElement = New XElement("{http://www.adventure-works.com}Root", _
     New XAttribute("{http://www.adventure-works.com}Att", "content") _
)

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.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.