xsltcontext xmlelement valor obtener nombres nodos nodo necesario leer especifico espacios administrador c# xpath

c# - xmlelement - Cómo acceder a un nodo xml con atributos y espacio de nombres usando selectsinglenode()



xmldocument c# (1)

Tengo este documento donde quiero obtener el valor en "x_server_response/retrieve_resources_by_category_response/source_full_info/record/ datafield[@tag=''520'']/subfield[@code=''a'']" Pero no puedo! ¿Por qué?

Sospecho que esto tiene algo que ver con la relación de espacio de nombres en el nodo de registro . Pero no puedo entender cómo hacerlo.

mi código se ve así:

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;

Y el xml dice así:

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


Porque parte de sus nodos se encuentran en el espacio de nombres "http://www.loc.gov/MARC21/slim/" , pero su XPath busca únicamente elementos en el espacio de nombres vacío.

Para solucionar esto, haga que el espacio de nombres sea conocido en su entorno invocando un administrador de espacio de nombres:

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;

EDITAR: aunque probablemente sea más fácil simplemente seleccionar

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

y deshazte del enfoque de dos pasos que tienes actualmente en total.