Question

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?

Was it helpful?

Solution

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"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top