2015-09-17 31 views
0

我有一個XML文檔,而且對於我來說,我無法使用XPath獲取數據。我試過了我能找到的每一個樣本,但沒有運氣。我正在嘗試提取電子郵件地址。有什麼想法嗎?XPath無法在XML中選擇

的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 protected]"/> 
        <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); 
+0

修復這個凌亂的格式,請.. – MethodMan

+0

這可能是命名空間有關。只要確保刪除xmlns =「urn:hl7-org:v3」,然後嘗試 – neo

+0

它確實與名稱空間部分有關。從文檔節點中刪除命名空間信息並且xpath代碼按照您的預期工作後,我嘗試了您的代碼。 –

回答

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

當我在OP的xml上使用這個例子時,我能夠獲得角色。 –

1

試試這個:

//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 protected]"/> element 
String xpathQuery = "//role//telecom[@value!='number']"; 

如果你要使用XPath來獲得一些屬性值,將XPathNavigator的富= xmlDocument.CreateNavigator後();你應該 使用selectSingleNode方法:

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