我怎么能在一个单一的节点建立XmlDocument的许多XML命名空间?能在、节点、我怎么、空间

2023-09-03 21:18:21 作者:酒与心事.

我试图建立的XmlDocument,这样序列化之后,我可以做到这样的XML:

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>

的事情之一是,允许的XmlElement通过的namespaceURI和preFIX性能只能指定一个命名空间。我怎样才能实现这种功能?

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

推荐答案

属性的xmlns:wnio和的xmlns:XF就像任何其他的属性。简单地将它们添加到您希望这些XML命名空间范围的的XmlElement。

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