【问题标题】:How can access to element or node to xml file from c#如何从 c# 访问元素或节点到 xml 文件
【发布时间】:2016-02-18 10:27:54
【问题描述】:

我有这个 xml 文件:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="CDASchemas\cda\Schemas\CCD.xsl"?>
<!-- The following sample document depicts a fictional character’s health summary. Any resemblance to a real person is coincidental. -->
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
    <!-- 
********************************************************
CDA Header
********************************************************
-->
    <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
    <templateId root="2.16.840.1.113883.10.20.1"/> <!-- CCD v1.0 Templates Root -->
    <!-- 
********************************************************
CDA Body
********************************************************
-->
    <component>
        <structuredBody>
<!-- 
********************************************************
Problems section
********************************************************
-->
<component>
<section>
    <templateId root='2.16.840.1.113883.10.20.1.11'/> <!-- Problem section template -->
    <code code="11450-4" codeSystem="2.16.840.1.113883.6.1"/> 
    <title>Problems</title> 
    <text>
        <table border="1" width="100%">
            <thead>
                <tr><th>Condition</th><th>Effective Dates</th><th>Condition Status</th></tr>
            </thead>
            <tbody>
                <tr><td>Asthma</td><td>1950</td><td>Active</td></tr>
                <tr><td>Pneumonia</td><td>Jan 1997</td><td>Resolved</td></tr>
                <tr><td>"</td><td>Mar 1999</td><td>Resolved</td></tr>
                <tr><td>Myocardial Infarction</td><td>Jan 1997</td><td>Resolved</td></tr>           
            </tbody>
        </table>
    </text>
    <entry typeCode="DRIV">
        <act classCode="ACT" moodCode="EVN">
            <templateId root='2.16.840.1.113883.10.20.1.27'/> <!-- Problem act template -->
            <id root="6a2fa88d-4174-4909-aece-db44b60a3abb"/>
            <code nullFlavor="NA"/>
            <entryRelationship typeCode="SUBJ">
                <observation classCode="OBS" moodCode="EVN">
                    <templateId root='2.16.840.1.113883.10.20.1.28'/> <!-- Problem observation template -->
                    <id root="d11275e7-67ae-11db-bd13-0800200c9a66"/>
                    <code code="ASSERTION" codeSystem="2.16.840.1.113883.5.4"/>                 
                    <statusCode code="completed"/> 
                    <effectiveTime><low value="1950"/></effectiveTime>
                    <value xsi:type="CD" code="195967001" codeSystem="2.16.840.1.113883.6.96" displayName="Asthma"/>
                    <entryRelationship typeCode="REFR">
                        <observation classCode="OBS" moodCode="EVN">
                            <templateId root='2.16.840.1.113883.10.20.1.50'/> <!-- Problem status observation template -->
                            <code code="33999-4" codeSystem="2.16.840.1.113883.6.1" displayName="Status"/>
                            <statusCode code="completed"/>
                            <value xsi:type="CE" code="55561003" codeSystem="2.16.840.1.113883.6.96" displayName="Active"/>
                        </observation>
                    </entryRelationship>
                </observation>
            </entryRelationship>
        </act>  
    </entry>


</section>
</component>
<section>
    <templateId root="2.16.840.1.113883.10.20.1.10"/> <!-- Plan of Care section template -->
    <code code="18776-5" codeSystem="2.16.840.1.113883.6.1"/>
    <title>Plan</title>
        <text>
        <table border="1" width="100%">
            <thead>
            <tr><th>Planned Activity</th><th>Planned Date</th></tr>
            </thead>
            <tbody>
            <tr><td>Pulmonary function test</td><td>April 21, 2000</td></tr>
            </tbody>
        </table>
    </text> 
    <entry typeCode="DRIV">
        <observation classCode="OBS" moodCode="RQO">
            <templateId root="2.16.840.1.113883.10.20.1.25"/> <!-- Plan of Activity activity template -->
            <id root="9a6d1bac-17d3-4195-89a4-1121bc809b4a"/>
            <code code="23426006" codeSystem="2.16.840.1.113883.6.96" displayName="Pulmonary function test"/>
            <statusCode code="new"/>
            <effectiveTime><center value="20000421"/></effectiveTime>
        </observation>
    </entry>
</section>
</component>
</structuredBody>
</component>
</ClinicalDocument>

我想解析这个文件并且我想访问它的一些元素。所以我已经构建了这段代码:

class Program
{
    static String _TEMPLATE_ID_PROBLEM = "2.16.840.1.113883.10.20.1.11";
    static String _xmlName = "D:\\Copy michele.castriotta@eresult (3).it\\Projects\\[2015 A] Decipher PCP\\02 - phase 1\\cda ccd\\HL7_CCD_final\\SampleCCDDocument.xml";
    static void Main(string[] args)
    {

        var doc = XDocument.Load(_xmlName);

        XElement root = doc.Root;

        //DEVO CERCARE DI SELEZIONARE LA SEZIONE GIUSTA
        IEnumerable<XElement>lista= root.XPathSelectElements("component/structuredBody/component/section");
        foreach (XElement e in lista)
        {
        //to do
        }
    }
}

但列表“lista”每次都是空的。哪里出错了?

【问题讨论】:

  • 您上面显示的 xml 似乎不是格式正确。你能检查一下吗?

标签: c# xml parsing


【解决方案1】:

问题出在命名空间上。根目录下的所有元素都取自与根目录标签相同的命名空间。

如果你已经为 Visual Studio 安装了 XPathInfo,然后从 VS 打开 xml 文件,你会注意到该部分的 xpath 信息如下所示:

/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='structuredBody'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']
/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3']

因此,你必须在调用中使用 NamespaceManager 来选择元素,并且你必须为每个元素指定命名空间的前缀:

var doc = XDocument.Load(_xmlName);

XElement root = doc.Root;
var namespaceManager = new XmlNamespaceManager(doc.CreateNavigator().NameTable);
namespaceManager.AddNamespace("hl7", "urn:hl7-org:v3");

//DEVO CERCARE DI SELEZIONARE LA SEZIONE GIUSTA

IEnumerable<XElement> lista = root.XPathSelectElements("hl7:component/hl7:structuredBody/hl7:component/hl7:section", namespaceManager);
foreach (XElement e in lista)
{
    //to do
}

【讨论】:

    【解决方案2】:

    它不是格式良好的 xml。你可以在这里试试xml validator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      相关资源
      最近更新 更多