【问题标题】:Need help to convert namespace code from XmlWriter to XmlDocument需要帮助将命名空间代码从 XmlWriter 转换为 XmlDocument
【发布时间】:2016-02-11 12:54:45
【问题描述】:

我现在尝试用 XmlWriter 将 4 行旧代码翻译成 XmlDocument 两个小时,但我失败了! ;(

XmlWriter.WriteStartDocument();
XmlWriter.WriteStartElement("document", "urn:hl7-org:v3");
XmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
XmlWriter.WriteAttributeString("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "urn:hl7-org:v3 GUDIDSPL.xsd");

我拥有的台词:

 XmlDeclaration xmlDeclaration = XmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
 XmlElement root = XmlDocument.CreateElement(string.Empty, "document", "urn:hl7-org:v3");
 XmlDocument.AppendChild(root);
 XmlDocument.InsertBefore(xmlDeclaration, root);

这是唯一有效的方法,之后的每一次代码和平,我都尝试过失败。 我没有得到 100% 正确的命名空间! 一个原因是“SetAttribute”,不提供前缀参数。

希望你能帮忙。

亲切的问候

跟进问题:

我用“har07”实现了代码postet,效果很好! 但我现在有不同的问题。

输出应如下所示:

< document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 GUDIDSPL.xsd" xmlns="urn:hl7-org:v3">
<id root="1fed661f-e015-4ea9-95e5-7cf293cd0517" />
<code code="C101716" codeSystem="2.16.840.1.113883.3.26.1.1" />
<effectiveTime xsi:type="TS" value="20160212" />

但它看起来像这样:

< document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 GUDIDSPL.xsd" xmlns="urn:hl7-org:v3">
<id root="6c4bb64e-d652-4fe6-80f1-8599196719d0" xmlns="" />
<code code="C101716" codeSystem="2.16.840.1.113883.3.26.1.1" xmlns="" />
<effectiveTime xsi:type="TS" value="20160212" xmlns="" />

我的创建元素代码总是生成这个空的 xmlns 标记。 我添加到我的 namespaceManager ("xsi", "http://www.w3.org/2001/XMLSchema-instance")

【问题讨论】:

  • 如果您不必使用 Xml 尝试将值转换为 JSON 数据
  • 出于代码卫生的考虑,请勿将XmlDocument 实例称为XmlDocument,即避免使用XmlDocument XmlDocument = new XmlDocument();。这在视觉上令人困惑。

标签: c# xml xmldocument xmlwriter


【解决方案1】:

使用SetAttribute(),可以直接指定前缀和属性本地名作为第一个参数:

....
XmlElement root = XmlDocument.CreateElement(string.Empty, "document", "urn:hl7-org:v3");
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("xsi:schemaLocation", "urn:hl7-org:v3 GUDIDSPL.xsd");
....

另一种选择是使用更新的 API,XDocument,而不是 XmlDocument

XNamespace d = "urn:hl7-org:v3";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var doc = 
    new XDocument(
        new XDeclaration("1.0", "UTF-8", null),
        new XElement(d + "document",  //create root element in default namespace
            new XAttribute("xmlns", d.ToString()), //add default namespace declaration
            new XAttribute(XNamespace.Xmlns + "xsi", xsi.ToString()), //add xsi namespace declaration
            new XAttribute(xsi + "schemaLocation", "urn:hl7-org:v3 GUDIDSPL.xsd") //add xsi:schemaLocation attribute
        )
    );

更新:

应该使用接受命名空间 uri 的 SetAttribute() 重载来定义命名空间中的属性,即 xsi:schemaLocation

....
XmlElement root = XmlDocument.CreateElement(string.Empty, "document", "urn:hl7-org:v3");
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "urn:hl7-org:v3 GUDIDSPL.xsd");
....

【讨论】:

  • 工作得很好,除了(出于什么原因!?)缺少 scemaLocation 之前的“xsi:”! ;(
  • @SharpNoiZy 我的错,检查 UPDATE 部分
  • 嗨,你能帮我解决一个后续问题吗?查看我的更新。
  • 谢谢,我可以试试,但我们最外层的元素是“文档”,它不是前缀。
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多