XPath does not work for selecting in XML

2019-09-01 10:18发布

问题:

I have an XML document and for the life of me I cannot get data by using XPath. I've tried every sample I could find and no luck. I'm trying to extract the email address. Any thoughts?

The 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>

To load it:

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

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

I've also tried:

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

But none of this works. Everything comes back empty. Any ideas? I can't seem to navigate past the root.

I've also tried adding the namespace manager in the selects, with no luck.

XmlNamespaceManager manager = new XmlNamespaceManager(xmlDocument.NameTable);

回答1:

You must add a namespace

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);


回答2:

Try this:

//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']"; 

If you are going to use XPath to get some attribute value, after the XPathNavigator foo = xmlDocument.CreateNavigator(); you should use the SelectSingleNode method:

//from the XPathNavigator object, we call the SelectSingleNode method, to select a single 
//node using the specified xpath query and then call the GetAttribute method to finally
//get the value attribute from that element
String email = foo.SelectSingleNode(xpathQuery).GetAttribute"value",nsmgr.DefaultNamespace);