我遇到了一个问题,当我以编程方式使用System.Xml类创建XML文档,然后使用Save时,输出XML不对节点使用QNames,而只使用本地名称。
期望输出
<ex:root>
<ex:something attr:name="value">
</ex:root>但我现在得到的是
<root>
<something name="value">
</root>这有点简化了,因为我使用的所有名称空间都是使用文档元素上的xmlns属性完全定义的,但为了清楚起见,我省略了这一点。
我知道XmlWriter类可以用来保存一个XmlDocument,这需要一个XmlWriterSettings类,但是我不知道如何配置这个类以获得完整的QNames输出。
发布于 2009-07-21 15:36:59
正如您所说,根元素需要名称空间定义:
<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:iis="http://schemas.microsoft.com/wix/IIsExtension">
<iis:WebSite Id="asdf" />
</Wix>上述xml的代码:
XmlDocument document = new XmlDocument();
document.AppendChild(document.CreateXmlDeclaration("1.0", null, null));
XmlNode rootNode = document.CreateElement("Wix", "http://schemas.microsoft.com/wix/2006/wi");
XmlAttribute attr = document.CreateAttribute("xmlns:iis", "http://www.w3.org/2000/xmlns/");
attr.Value = "http://schemas.microsoft.com/wix/IIsExtension";
rootNode.Attributes.Append(attr);
rootNode.AppendChild(document.CreateElement("iis:WebSite", "http://schemas.microsoft.com/wix/IIsExtension"));
document.AppendChild(rootNode);将命名空间uri作为参数传递给CreateAttribute和CreateElement方法的要求似乎有悖于直觉,因为可以认为文档能够派生出这些信息,但是,这正是它的工作方式。
https://stackoverflow.com/questions/1159771
复制相似问题