2009-10-20 12 views
5

मैं नहीं समझ सकता क्यों यह NodeList खाली हैXmlNodeList

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

यहाँ 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/"> 
    <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> 

अंतिम लिपि में (यह क्यों खाली है) मैं एक सरणी स्ट्रिंग जिसमें प्राप्त करना चाहते हैं इसमें हर विशेषता।

धन्यवाद स्टीफन

उत्तर

3

उपयोगकर्ता marc_s का जवाब वास्तव में सही है। आपको एक्सएमएल नेमस्पेस पर ध्यान देना होगा। हालांकि, उनका कोड नमूना सीधे आपके उदाहरण के लिए काम नहीं करेगा। यहां एक पूर्ण नमूना है जो आपके द्वारा दिए गए एक्सएमएल के साथ काम करता है (हालांकि मुझे इसे साफ़ करना था ... 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 

मैं सुझाव देता हूं कि एक्सएमएल नेमस्पेस का थोड़ा और अध्ययन कर रहा हूं। Ronald Bourret's "XML Namespaces FAQ" स्किमिंग का प्रयास करें।

+0

+1 एक्सएमएल नेमस्पेस FAQ के लिए अच्छा लिंक! धन्यवाद। –

8

आप खाते में दस्तावेज़ पर 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); 

मार्क

+0

यह 0 भी है, मैंने हर बार "xmlns" को अनदेखा किया, इसलिए मैं उपरोक्त XML फ़ाइल को संपादित करता हूं। – sschnake

+0

आपने मुझे सही तरीके से लाया। टैंक आप – sschnake

+0

खुशी है कि मैं मदद कर सकता हूं। –

0

प्रयास करें:

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

+0

नहीं .. खाली। तो मैं अब पूर्ण कोड पोस्ट करूंगा – sschnake