سؤال

When I try to code as this,

XPathNodeIterator it;

string selectStr = "Equipment/Group/item";
it = nav.Select(selectStr);
while (it.MoveNext())
{
    String strVal = "";
    if (it.Current.HasChildren)
    {
        strVal = it.Current.Value.ToString(); // get wrong value here. I want to get 'COM' and 'MD'
    }
}

My Xml File as this

<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <License licenseId="" licensePath=""/>
  <Group>
    <item>
      <key>COM</key>
      <value>
        <format>10</format>
        <value>0</value>
      </value>
    </item>
    <item>
      <key>MD</key>
      <value>
        <format>20</format>
        <value>test</value>
      </value>
    </item>
.....

I found when I use while method to loop all the xml file is very convenient.

But I don't know how to establish the relation to get element value when I defined an XPathNodeIterator as above?

Actually, I need read the whole Xml file with the aid of XPathNodeIterator.

هل كانت مفيدة؟

المحلول

You would have to start a new iterator for each subnode selecting only the subitems. XPathNodeIterator and XPath in general is more geared towards extracting data from xml. If you want to read the whole XML file, you should consider XmlReader (very fast, forward only) or XDocument (easy to use).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top