How can I create an XmlDocument with many xml namespaces in one node?

I am trying to create an XmlDocument so that after serialization I can 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 fact is that XmlElement allows you to specify only one namespace through the NamespaceURI properties and the prefix. How can I perform this function?

+3
source share
1 answer

The attributes "xmlns: wnio" and "xmlns: xf" are attributes like any other. Just add them to the XmlElement so you want these XML namespaces to be available.

The following snippet gives almost 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"));
+3
source

Source: https://habr.com/ru/post/1710985/


All Articles