30Nov/090
Adding Multiple Namespaces to an XDocument/XElement
Lets say you want 3 namespaces added to your document.
You would define each one with the XNamespace property:
XNamespace defaultNS = "urn://xml.voodoo.net/vd/vd-1.0"; XNamespace a = "urn://xml.voodoo.net/vd/activity-1.0"; XNamespace f = "urn://xml.voodoo.net/vd/formating-1.0";
One of the namespaces will be your default.
When you create your rood element, you first add the default and then the other after.
The other 2 are added as you see below:
XElement rootElement = new XElement(defaultNS + "root", new XAttribute(XNamespace.Xmlns + "f", f.NamespaceName), new XAttribute(XNamespace.Xmlns + "a", a.NamespaceName));
To access the other namespaces, you
XElement somethingElement = new XElement(a + "something"); somethingElement.Value = "stuff and more stuff"; rootElement.Add(somethingElement);
The result will look like this:
<root xmlns:f="urn://xml.voodoo.net/vd/formating-1.0" xmlns:a="urn://xml.voodoo.net/vd/activity-1.0" xmlns="urn://xml.voodoo.net/vd/vd-1.0">
<a:something>stuff and more stuff</a:something>
</root>