views:

294

answers:

5

I can't understand why this NodeList is Empty

XmlDocument document = new XmlDocument();
document.Load(xmlpath);    
XmlNodeList nodes = document.SelectNodes("/StructureResponse/rootItem/attributes/Attribute");

Here the XmlFile

<?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/"&gt;
 <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/"&gt;
  <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>

In the Final Script I want to get an array string which contains every Attribute in it.

Thank you Stefan

+7  A: 

You're not taking into account the XML namespace (xmlns="http://nts-de-osm1-pxc/webservices/") on the document!

OK, you even have two separate namespaces - updated my sample.

Try this:

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

Marc

marc_s
It's even 0I ignored the "xmlns" every time, so i edit the XML File above.
sschnake
You brought me on the right way. tank you
sschnake
Glad I could help.
marc_s
A: 

Try:

XmlNodeList nodes = document.SelectNodes("./StructureResponse/rootItem/attributes");

Kyle Rozendo
No .. Empty.So I will post the Full Code now
sschnake
+1  A: 

User marc_s's answer is actually correct. You need to pay attention to the XML namespaces. His code sample, however, will not work directly for your example. Here is a full sample that works with the XML you gave (although I had to clean it up... it was missing a closing tag for 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/'&gt;
    <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/'&gt;
      <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

I suggest doing a bit more studying of XML namespaces. Try skimming Ronald Bourret's "XML Namespaces FAQ".

bobbymcr
+1 good link for XML namespaces FAQ ! THanks.
marc_s
A: 

I got it: XML File:

<?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/"&gt;
        <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/"&gt;
                <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>

Code:

document.Load(xmlpath);

            XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);
            mgr.AddNamespace("webservices", "http://nts-de-osm1-pxc/webservices/");
            mgr.AddNamespace("service", "http://systinet.com/wsdl/com/osm/webservices/service/");


            XmlNodeList nodes = document.SelectNodes("/webservices:StructureResponse/service:rootItem/service:attributes/service:Attribute", mgr);

The answer from Marc_s worked fine. Tank You.

sschnake
A: 

This is great! The best solution that i ever came across.

Alice