How to: Create a Document with Namespaces (C#) (LINQ to XML)
This topic shows how to create documents with namespaces.
Example
To create an element or an attribute that is 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 = "https://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="https://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 = "https://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "https://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
This example produces the following output:
<aw:Root xmlns:aw="https://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. Another is a namespace with a prefix.
By including namespace attributes in the root element, the namespaces are serialized so that https://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.
// The https://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "https://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "https://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 XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
This example produces the following output:
<Root xmlns="https://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<DifferentChild>other content</DifferentChild>
</fc:Child>
<Child2>c2 content</Child2>
<fc:Child3>c3 content</fc:Child3>
</Root>
The following example creates a document that contains two namespaces, both with namespace prefixes.
XNamespace aw = "https://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="https://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 (LINQ to XML) for more information
// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{https://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "https://www.adventure-works.com"),
new XElement("{https://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);
This example produces the following output:
<aw:Root xmlns:aw="https://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>