如何使用choicesinglenode()访问具有属性和命名空间的xml节点

我有这个文件,我希望得到"x_server_response/retrieve_resources_by_category_response/source_full_info/record/ datafield[@tag='520']/subfield[@code='a']"但我不能! 为什么?

我怀疑这与记录节点上的命名空间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是这样的:

   metalib_version="4.00 (20)>   CKB02166  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    3B7F9EQE259KNK1YUK462VCCG4455T4BUPUC5B9LVQS9XD16U6  

因为部分节点位于"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'] 

并摆脱你目前完全采取的两步法。