Xmlnodelist (لماذا هذا فارغ)
-
22-09-2019 - |
سؤال
لا أستطيع أن أفهم سبب فارغ هذا الإيماءة
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>
في البرنامج النصي النهائي ، أريد الحصول على سلسلة صفيف تحتوي على كل سمة فيه.
شكرا لك ستيفان
المحلول
إجابة المستخدم 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");