简体   繁体   中英

How can I build XmlDocument with many xml namespaces in a single node?

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?

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM