Events
Mar 17, 9 PM - Mar 21, 10 AM
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.
This article shows how to create documents in C# that have namespaces.
To create an element or an attribute that's in a namespace, you first declare and initialize an XNamespace object. You then use the addition operator overload to combine the namespace with the local name, expressed as a string.
The following example creates a document with one namespace. By default, LINQ to XML serializes this document with a default namespace.
// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
This example produces the following output:
<Root xmlns="http://www.adventure-works.com">
<Child>child content</Child>
</Root>
The following example creates a document with one namespace. It also creates an attribute that declares the namespace with a namespace prefix. To create an attribute that declares a namespace with a prefix, you create an attribute where the name of the attribute is the namespace prefix, and this name is in the Xmlns namespace. The value of this attribute is the URI of the namespace.
// Create an XML tree in a namespace, with a specified prefix
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
This example produces the following output:
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
The following example shows the creation of a document that contains two namespaces. One is the default namespace, the other is a namespace with a prefix.
By including namespace attributes in the root element, the namespaces are serialized so that http://www.adventure-works.com
is the default namespace, and www.fourthcoffee.com
is serialized with a prefix of fc
. To create an attribute that declares a default namespace, you create an attribute with the name xmlns
, without a namespace. The value of the attribute is the default namespace URI.
If a default namespace declaration is in scope, it applies to child XElement
objects by prefixing their local names with the corresponding XNamespace
object. On the other hand, default namespace declarations do not apply directly to attribute names. So, XAttribute
objects in the default namespace are defined by not prefixing their local name with the corresponding XNamespace
object.
// The http://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "http://www.adventure-works.com"),
new XAttribute(XNamespace.Xmlns + "fc", "www.fourthcoffee.com"),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content",
new XAttribute("DefaultNs", "default namespace"),
new XAttribute(fc + "PrefixedNs", "prefixed namespace")
),
new XElement(fc + "Child3", "c3 content",
new XAttribute("DefaultNs", "default namespace"),
new XAttribute(fc + "PrefixedNs", "prefixed namespace")
)
);
Console.WriteLine(root);
This example produces the following output:
<Root xmlns="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<DifferentChild>other content</DifferentChild>
</fc:Child>
<Child2 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c2 content</Child2>
<fc:Child3 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c3 content</fc:Child3>
</Root>
The following example creates a document that contains two namespaces, both with namespace prefixes.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", aw.NamespaceName),
new XAttribute(XNamespace.Xmlns + "fc", fc.NamespaceName),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
This example produces the following output:
<aw:Root xmlns:aw="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<aw:DifferentChild>other content</aw:DifferentChild>
</fc:Child>
<aw:Child2>c2 content</aw:Child2>
<fc:Child3>c3 content</fc:Child3>
</aw:Root>
Another way to accomplish the same result is to use expanded names instead of declaring and creating an XNamespace object.
This approach has performance implications. Each time you pass a string that contains an expanded name to LINQ to XML, LINQ to XML must parse the name, find the atomized namespace, and find the atomized name. This process takes CPU time. If performance is important, you might want to declare and use an XNamespace object explicitly.
If performance is an important issue, see Pre-Atomization of XName Objects for more information.
// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{http://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement("{http://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);
This example produces the following output:
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now