0

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);
heguchi
  • 27
  • 10
James F
  • 535
  • 9
  • 26
  • fix this messy formatting please.. – MethodMan Sep 17 '15 at 20:19
  • It might be namespace related. Just to make sure remove xmlns="urn:hl7-org:v3" and then try – neo Sep 17 '15 at 20:45
  • It does have something to do with the name space part. I tried your code after removing the namespace information from the document node and the xpath code worked as you expected. – David Parvin Sep 17 '15 at 23:20
  • 1
    possible duplicate of [SelectSingleNode returning null for known good xml node path using XPath](http://stackoverflow.com/questions/1089196/selectsinglenode-returning-null-for-known-good-xml-node-path-using-xpath) – har07 Sep 18 '15 at 01:08

2 Answers2

2

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);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
1

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); 
heguchi
  • 27
  • 10