如何从 XPathNodeIterator 获取元素值?
当我尝试这样编码时,
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 文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须为每个子节点启动一个新的迭代器,仅选择子项。一般来说,
XPathNodeIterator
和 XPath 更适合从 xml 中提取数据。如果您想读取整个 XML 文件,您应该考虑 XmlReader< /a> (非常快,仅转发)或 XDocument< /a>(易于使用)。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).