XPath不适用于在XML中进行选择

时间:2015-09-17 20:16:28

标签: c# xml xpath xmldocument

我有一个XML文档,但就我而言,我无法使用XPath获取数据。我已经尝试了我能找到的每一个样本而且没有运气。我试图提取电子邮件地址。有什么想法吗?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3" xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
    <typeId root="test1" extension="test2"/>
        <id root="test3" extension="test4"/>
            <code code="test5" codeSystem="test6" />
            <effectiveTime value="201509171214"/>
            <confidentialityCode code="N" codeSystem="test7" codeSystemName="test8" displayName="normal"/>
            <languageCode code="en"/>
            <recordTarget>
                <Role>
                    <id root="000000" extension="number1"/>
                    <id root="11111" extension="number2"/>
                    <addr>
                        <streetAddressLine>Street</streetAddressLine>
                        <postalCode>12345</postalCode>
                        <city>City</city>
                        <state>STATE</state>
                        <country>COUNTRY</country>
                    </addr>
                    <telecom value="number" use="HP"/>
                    <telecom value="number" use="MC"/>
                    <telecom value="email@email"/>
                    <person>
                        <name>
                            <family>family</family>
                            <given>given</given>
                            <prefix/>
                            <suffix/>
                        </name>
                        <administrativeGenderCode code="C" codeSystem="code" codeSystemName="code name" displayName="c"/>
                        <birthTime value="N/A"/>
                    </person>
                </Role>
            </recordTarget>
</Document>

加载它:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(STRING DATA FROM XML);

XPathNavigator foo = xmlDocument.CreateNavigator();
foo.MoveToFollowing(XPathNodeType.Element);
foo.Select("Document/recordTarget/Role");

我也尝试过:

XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/Document/recordTarget/Role");

但这一切都无效。一切都回来了。有任何想法吗?我似乎无法越过根。

我还尝试在选择中添加命名空间管理器,但没有运气。

XmlNamespaceManager manager = new XmlNamespaceManager(xmlDocument.NameTable);

2 个答案:

答案 0 :(得分:1)

试试这个:

inotifywait -r -e modify -t 60 /path/to/folder

如果要使用XPath获取某些属性值,请在XPathNavigator foo = xmlDocument.CreateNavigator()之后;你应该 使用SelectSingleNode方法:

//Create a namespacemanager to get the defaultnamespace of the xml document    
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);

//XPath to find the tag that the value isn't equals to number, in this case, it will 
//return the <telecom value="email@email"/> element
String xpathQuery = "//role//telecom[@value!='number']"; 

答案 1 :(得分:1)

您必须添加名称空间

XPathNavigator navigator = xmlDocument.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("ns", "urn:hl7-org:v3");

var role = navigator.Select("/ns:Document/ns:recordTarget/ns:Role", manager);