2009-11-11 72 views
1

我有這個文檔,我想要的值在"x_server_response/retrieve_resources_by_category_response/source_full_info/record/ datafield[@tag='520']/subfield[@code='a']" 但我不能!爲什麼?如何使用屬性和名稱空間使用Selectinginglenode訪問XML節點()

我懷疑這與記錄節點上的命名空間daclaration有關。但我無法弄清楚如何去做。

我的代碼看起來是這樣的:

XmlNodeList xmlResources = r.ResponseXmlDocument.SelectNodes("x_server_response/retrieve_resources_by_category_response/source_full_info);    

      foreach (XmlNode xmlResource in xmlResources) 
      { 
       string information = xmlResource.SelectSingleNode("record/datafield[@tag='520']/subfield[@code='a']").InnerText; 

和XML是這樣的:

<x_server_response> metalib_version="4.00 (20)> 
    <source_full_info> 
     <record xmlns="http://www.loc.gov/MARC21/slim/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.loc.gov/MARC21/slim 
     http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"> 
     <controlfield tag="001">CKB02166</controlfield> 
       <datafield tag="520" ind1=" " ind2=" "> 
     <subfield code="a">Providing access to thousands of online journals from leading 
     scholarly, academic and business publishers, the Ingenta Select service provides fast and 
     reliable access from a global network of servers to users' desktops around the world. 
     ## ##Ingenta Select provides access to more than 5,000 electronic 
     publications from over 190 publisher clients and bring together an extensive range of services 
     for the librarian and end-user alike</subfield> 
     </datafield>   </record> 
     </source_full_info> 
     <session_id new_session="N">3B7F9EQE259KNK1YUK462VCCG4455T4BUPUC5B9LVQS9XD16U6</session_id> 
<x_server_response> 

回答

5

因爲你的節點的一部分,是在"http://www.loc.gov/MARC21/slim/"命名空間,但是你的XPath查找中的元素只有空的命名空間。

XmlNamespaceManager nsmgr = new XmlNamespaceManager(r.ResponseXmlDocument); 
nsmgr.AddNamespace("marc", "http://www.loc.gov/MARC21/slim/"); 
string xpath = "marc:record/marc:datafield[@tag='520']/marc:subfield[@code='a']"; 

// ... 
string information = xmlResource.SelectSingleNode(xpath).InnerText; 

編輯:

通過調用一個命名空間管理解決這個問題,讓知道您的環境中的命名空間。雖然它可能是更容易地只選擇

//marc:datafield[@tag='520']/marc:subfield[@code='a'] 

和擺脫你現在完全採用兩步法。

+0

這正是我所期待的。我不知道你可以在不提供名稱表的情況下添加一個名稱空間。 明天早上我會試試這個第一件事。謝謝。 – Fontanka16 2009-11-11 21:32:13

相關問題