【问题标题】:Read node value from input xml file and write it on other output xml file从输入 xml 文件中读取节点值并将其写入其他输出 xml 文件
【发布时间】:2016-02-25 10:13:36
【问题描述】:

我有一个包含以下标签的 xml 文件。

 <ClinicalDocument xmlns:ext="http://ns.electronichealth.net.au/Ci/Cda/Extensions/3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3">

现在我必须阅读这个并将属性值写入一个字符串。

现在我正在使用以下代码。

XmlDocument xDocument = new XmlDocument();
xDocument.Load(@"c:\Test.xml");
System.Text.StringBuilder ClinicalDocument = new System.Text.StringBuilder();
ClinicalDocument.Append("<ClinicalDocument xmlns:ext=" + xDocument.SelectSingleNode("/ClinicalDocument/@xmlns:ext").Value + " xmlns:xsi=" + xDocument.SelectSingleNode("/ClinicalDocument/@xmlns:xsi").Value + " xmlns=" + xDocument.SelectSingleNode("/ClinicalDocument/@xmlns").Value + ">");
Response.Write(ClinicalDocument);

但其抛出异常“需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。”

请提出我做错了什么或有其他选择。我对 XML 完全陌生。

【问题讨论】:

    标签: c# xml c#-4.0 xml-parsing


    【解决方案1】:

    您要获取的不是普通属性,而是名称空间属性。您可以尝试使用 XPath namespace 轴来获取命名空间属性,如下所示:

    /*/namespace::ext
    

    工作演示示例:

    var xml = @"<ClinicalDocument xmlns:ext='http://ns.electronichealth.net.au/Ci/Cda/Extensions/3.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:hl7-org:v3'/>";
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    var result = doc.SelectSingleNode("/*/namespace::ext");
    Console.WriteLine(result.Value);
    

    Dotnetfiddle Demo

    输出:

    http://ns.electronichealth.net.au/Ci/Cda/Extensions/3.0
    

    【讨论】:

    • 嘿。谢谢。这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2013-05-25
    相关资源
    最近更新 更多