views:

71

answers:

1

I'm trying to build XmlDocument so that after serialization I could achieve something like this xml:

<?xml version="1.0" encoding="UTF-8"?>
<wnio:element xmlns:wnio="somuri" xmlns:xf="abcd">
   <xf:nestedelement>somtext</xf:nestedelement>
</wnio:element>

The things is that XmlElement allows to specify ONLY ONE namespace via NamespaceURI and Prefix properties. How can I accomplish this kind of functionality?

+2  A: 

The attributes "xmlns:wnio" and "xmlns:xf" are attributes like any other. Simply add them to the XmlElement that you would like these XML Namespaces to scope to.

The following snippet produces almost exactly what you want:

XmlDocument document = new XmlDocument();
document.AppendChild(document.CreateElement("wnio", "element", "somuri"));
document.DocumentElement.SetAttribute("xmlns:xf", "abcd");
document.DocumentElement.AppendChild(document.CreateElement("xf", "nestedelement", "abcd"));
Dave Van den Eynde