首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XmlDocument.Save没有为元素输出完整的QNames

XmlDocument.Save没有为元素输出完整的QNames
EN

Stack Overflow用户
提问于 2009-07-21 15:04:16
回答 1查看 544关注 0票数 0

我遇到了一个问题,当我以编程方式使用System.Xml类创建XML文档,然后使用Save时,输出XML不对节点使用QNames,而只使用本地名称。

期望输出

代码语言:javascript
复制
<ex:root>
  <ex:something attr:name="value">
</ex:root>

但我现在得到的是

代码语言:javascript
复制
<root>
  <something name="value">
</root>

这有点简化了,因为我使用的所有名称空间都是使用文档元素上的xmlns属性完全定义的,但为了清楚起见,我省略了这一点。

我知道XmlWriter类可以用来保存一个XmlDocument,这需要一个XmlWriterSettings类,但是我不知道如何配置这个类以获得完整的QNames输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-07-21 15:36:59

正如您所说,根元素需要名称空间定义:

代码语言:javascript
复制
<?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的代码:

代码语言:javascript
复制
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方法的要求似乎有悖于直觉,因为可以认为文档能够派生出这些信息,但是,这正是它的工作方式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1159771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档