如何使用selectsinglenode()访问具有属性和名称空间的xml节点 [英] How to access a xml node with attributes and namespace using selectsinglenode()

查看:67
本文介绍了如何使用selectsinglenode()访问具有属性和名称空间的xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此文档,我想获取"x_server_response/retrieve_resources_by_category_response/source_full_info/record/datafield [@ tag ='520']/subfield [@ code ='a']" 但是我做不到!为什么?

I have this document where i want to get to the value in "x_server_response/retrieve_resources_by_category_response/source_full_info/record/ datafield[@tag='520']/subfield[@code='a']" But i just can't! Why?

我怀疑这与 record 节点上的名称空间声明有关.但是我不知道该怎么做.

i suspect that this has something to do with the namespace daclaration at the record node. But i can not figure out how to do it.

我的代码如下:

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> 

推荐答案

由于部分节点位于"http://www.loc.gov/MARC21/slim/" 命名空间中,但是您的XPath仅在空名称空间中查找元素.

Because part of your nodes are in the "http://www.loc.gov/MARC21/slim/" namespace, but your XPath looks for elements in the empty namespace only.

要解决此问题,请通过调用名称空间管理器使您的环境知道该名称空间:

To fix this, make the namespace known to your environment by invoking a namespace manager:

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']

摆脱现有的分两步走的方法.

and get rid of the two-step approach you currently have altogether.

这篇关于如何使用selectsinglenode()访问具有属性和名称空间的xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆