XmlNodeList (почему это пустое значение)
-
22-09-2019 - |
Вопрос
Я не могу понять, почему этот список узлов пуст
XmlDocument document = new XmlDocument();
document.Load(xmlpath);
XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute");
Вот XmlФайл
<?xml version="1.0" encoding="utf-8"?>
<StructureResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://nts-de-osm1-pxc/webservices/">
<consolidatedItems xsi:nil="true" xmlns="http://systinet.com/wsdl/com/osm/webservices/service/" />
<rootItem xsi:type="Part" xmlns="http://systinet.com/wsdl/com/osm/webservices/service/">
<attributes>
<Attribute>
<dataDictionary xsi:nil="true" />
<dataType>string</dataType>
<displayName>IDENT_NR</displayName>
<key>true</key><name>IDENT_NR</name>
<searchable>true</searchable>
<userAttribute>true</userAttribute>
<value>9662744</value>
</Attribute>
<Attribute>
<dataDictionary xsi:nil="true" />
<dataType>string</dataType>
<displayName>AI</displayName>
<key>true</key><name>AI</name>
<searchable>true</searchable>
<userAttribute>true</userAttribute>
<value>00</value>
</Attribute>
</rootItem>
</StructureResponse>
В конечном скрипте я хочу получить строку массива, которая содержит каждый атрибут в нем.
Спасибо Стефан
Решение
Ответ пользователя marc_s на самом деле правильный.Вам нужно обратить внимание на пространства имен XML.Однако его пример кода не будет работать непосредственно для вашего примера.Вот полный образец, который работает с предоставленным вами XML (хотя мне пришлось его почистить...в нем отсутствовал закрывающий тег для attributes
).
string xmlData =
@"<?xml version='1.0' encoding='utf-8'?>
<StructureResponse
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://nts-de-osm1-pxc/webservices/'>
<consolidatedItems xsi:nil='true' xmlns='http://systinet.com/wsdl/com/osm/webservices/service/' />
<rootItem xsi:type='Part' xmlns='http://systinet.com/wsdl/com/osm/webservices/service/'>
<attributes>
<Attribute>
<dataDictionary xsi:nil='true' />
<dataType>string</dataType>
<displayName>IDENT_NR</displayName>
<key>true</key>
<name>IDENT_NR</name>
<searchable>true</searchable>
<userAttribute>true</userAttribute>
<value>9662744</value>
</Attribute>
<Attribute>
<dataDictionary xsi:nil='true' />
<dataType>string</dataType>
<displayName>AI</displayName>
<key>true</key>
<name>AI</name>
<searchable>true</searchable>
<userAttribute>true</userAttribute>
<value>00</value>
</Attribute>
</attributes>
</rootItem>
</StructureResponse>";
XmlDocument document = new XmlDocument();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("a", "http://nts-de-osm1-pxc/webservices/");
namespaceManager.AddNamespace("b", "http://systinet.com/wsdl/com/osm/webservices/service/");
document.LoadXml(xmlData);
XmlNodeList nodes = document.SelectNodes("/a:StructureResponse/b:rootItem/b:attributes/b:Attribute", namespaceManager);
// 'nodes' contains 2 items now, as expected
Я предлагаю немного подробнее изучить пространства имен XML.Попробуйте снять сливки "Часто задаваемые вопросы по пространствам имен XML" Рональда Бурре.
Другие советы
Вы не принимаете во внимание пространство имен XML (xmlns="http://nts-de-osm1-pxc/webservices/"
) в документе!
Хорошо, у вас даже есть два отдельных пространства имен - обновил мой пример.
Попробуй это:
XmlDocument document = new XmlDocument();
document.Load(xmlpath);
XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);
mgr.AddNamespace("ns", "http://nts-de-osm1-pxc/webservices/");
mgr.AddNamespace("root", "http://systinet.com/wsdl/com/osm/webservices/service/");
XmlNodeList nodes = document.SelectNodes("/ns:StructureResponse/root:rootItem/root:attributes/root:Attribute", mgr);
Марк
Попробуй:
XmlNodeList nodes = document.SelectNodes("./StructureResponse/rootItem/attributes");