Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
XML namespaces associate element and attribute names in an XML document with custom and predefined URIs. To create these associations, you define prefixes for namespace URIs, and use those prefixes to qualify element and attribute names in XML data. Namespaces prevent element and attribute name collisions, and enable elements and attributes of the same name to be handled and validated differently.
To declare a namespace on an element, you use the xmlns:
attribute:
xmlns:<name>=<"uri">
where <name>
is the namespace prefix and <"uri">
is the URI that identifies the namespace. After you declare the prefix, you can use it to qualify elements and attributes in an XML document and associate them with the namespace URI. Because the namespace prefix is used throughout a document, it should be short in length.
This example defines two BOOK
elements. The first element is qualified by the prefix, mybook
, and the second element is qualified by the prefix, bb
. Each prefix is associated with a different namespace URI:
<mybook:BOOK xmlns:mybook="http://www.contoso.com/books.dtd">
<bb:BOOK xmlns:bb="urn:blueyonderairlines" />
</mybook:BOOK>
To signify that an element is a part of a particular namespace, add the namespace prefix to it. For example, if an Author
element belongs to the mybook
namespace, it is declared as <mybook:Author>
.
A namespace is effective from its point of declaration until the end of the element it was declared in. In this example, the namespace defined in the BOOK
element doesn't apply to elements outside the BOOK
element, such as the Publisher
element:
<Author>Joe Smith</Author>
<BOOK xmlns:book="http://www.contoso.com">
<title>My Wonderful Day</title>
<price>$3.95</price>
</BOOK>
<Publisher>
<Name>MSPress</Name>
</Publisher>
A namespace must be declared before it can be used, but it doesn't have to appear at the top of the XML document.
When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.
To use the default namespace, omit the prefix and the colon from the declaration on the element:
<BOOK xmlns="http://www.contoso.com/books.dtd">
...
</BOOK>
The XmlNamespaceManager class stores a collection of namespace URIs and their prefixes, and lets you look up, add, and remove namespaces from this collection. In certain contexts, this class is required for better XML processing performance. For example, the XsltContext class uses XmlNamespaceManager for XPath support.
The namespace manager doesn't perform any validation on the namespaces, but assumes that prefixes and namespaces have already been verified and conform to the W3C Namespaces specification.
Note
LINQ TO XML in C# and Visual Basic don't use XmlNamespaceManager to manage namespaces. See Working with XML Namespaces (C#) and Working with XML Namespaces (Visual Basic) in the LINQ documentation for information about managing namespaces when using LINQ to XML.
Here are some of the management and lookup tasks you can perform with the XmlNamespaceManager class. For more information and examples, follow the links to the reference page for each method or property.
To | Use |
---|---|
Add a namespace | AddNamespace method |
Remove a namespace | RemoveNamespace method |
Find the URI for the default namespace | DefaultNamespace property |
Find the URI for a namespace prefix | LookupNamespace method |
Find the prefix for a namespace URI | LookupPrefix method |
Get a list of namespaces in the current node | GetNamespacesInScope method |
Scope a namespace | PushScope and PopScope methods |
Check whether a prefix is defined in the current scope | HasNamespace method |
Get the name table used to look up prefixes and URIs | NameTable property |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowDocumentation
How to control namespace prefixes - LINQ to XML - .NET
You can control namespace prefixes when serializing an XML tree in C# and Visual Basic. To do this, insert attributes that declare namespaces.
How to create a document with namespaces in C# - LINQ to XML - .NET
Use the XNamespace object in C# to create documents that have default namespaces or namespaces with a prefix.
Changing Namespace Declarations in an XML Document - .NET
Learn more about: Changing Namespace Declarations in an XML Document
Changing Namespace Prefix Properties - .NET
Learn more about: Changing Namespace Prefix Properties