【问题标题】:How can I get element value from XPathNodeIterator?如何从 XPathNodeIterator 获取元素值?
【发布时间】:2010-12-08 09:26:45
【问题描述】:

当我尝试这样编码时,

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'
    }
}

我的 Xml 文件就是这样

<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>
.....

我发现当我使用while方法循环所有的xml文件时非常方便。

但是当我像上面定义一个 XPathNodeIterator 时,我不知道如何建立关系以获取元素值?

实际上,我需要借助 XPathNodeIterator 读取整个 Xml 文件。

【问题讨论】:

    标签: c# xml c#-2.0 xpathnodeiterator


    【解决方案1】:

    您必须为每个仅选择子项的子节点启动一个新的迭代器。 XPathNodeIteratorXPath 通常更适合从 xml 中提取数据。如果你想阅读整个 XML 文件,你应该考虑XmlReader(非常快,只转发)或XDocument(易于使用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-13
      • 2021-05-28
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      相关资源
      最近更新 更多