使用XPathNodeIterator循环遍历xml的问题 [英] Problem looping through xml using XPathNodeIterator
问题描述
我有以下xml:
< items>
< item>
< id> 1< / id>
< name> Item1< / name>
< types>
< type>&
< id> 1< / id>
< / type>
< type>&
< id> 2< / id>
< / type
< / types>
< / item>
< item>
< id> 2< / id>
< name> Item2< / name>
< types>
< type>&
< id> 2< / id>
< / type
< / types>
< / item>
< / items>
我有以下代码:
Dim xPathNavigator
< span style ="font-size:x-small; color:#0000ff"> As XPathNavigator = xpd.CreateNavigator()
Dim xPathExpression
< span style ="font-size:x-small; color:#0000ff"> As XPathExpression = xPathNavigator.Compile(" / items / item")
昏暗 xni
As XPathNodeIterator = xPathNavigator。[Select](xPathExpression)
xPathExpression = xPathNavigator.Compile(" / items / item / types / type")
选择
案例 xPathExpression.ReturnType
案例 XPathResultType。[String]
退出
选择
xni.MoveNext()
Response.Write(xPathNavigator。[Select](xPathExpression))
案例 XPathResultType.NodeSet
虽然 xni.MoveNext()
昏暗 i
As XPathNodeIterator = xni.Current.SelectChildren(" id","")
当 i.MoveNext()
如果 i.Current.Value = 1
然后
xni = xPathNavigator。[Select](xPathExpression)
'处理
xni.Current.MoveToParent ()
xni.Current.MoveToFirstAttribute()
结束 虽然
结束
虽然
退出
选择
我希望能够做的只是当type = 1属于类型集合时才返回名称。 I可以迭代整个xml工作表中的所有类型,但不只是一个项目的
。
您好,
您可以在XPath中表达:
Dim doc = New XmlDocument()
doc.Load(" myxml.xml")
For Each node As XmlNode in doc.SelectNodes(" / items / item [types / type / id = 1] / name")
Console.WriteLine(node.InnerText)
Next谢谢,
I have the following xml:
<items>
<item>
<id>1</id>
<name>Item1</name>
<types>
<type>
<id>1</id>
</type>
<type>
<id>2</id>
</type
</types>
</item>
<item>
<id>2</id>
<name>Item2</name>
<types>
<type>
<id>2</id>
</type
</types>
</item>
</items>
I have the following code:
Dim xPathNavigator
As XPathNavigator = xpd.CreateNavigator()
Dim xPathExpression
As XPathExpression = xPathNavigator.Compile("/items/item")
Dim xni
As XPathNodeIterator = xPathNavigator.[Select](xPathExpression)
xPathExpression = xPathNavigator.Compile("/items/item/types/type")
Select
Case xPathExpression.ReturnType
Case XPathResultType.[String]
Exit
Select
xni.MoveNext()
Response.Write(xPathNavigator.[Select](xPathExpression))
Case XPathResultType.NodeSet While xni.MoveNext() Dim i As XPathNodeIterator = xni.Current.SelectChildren("id", "") While i.MoveNext() If i.Current.Value = 1 Then
xni = xPathNavigator.[Select](xPathExpression)
' Do processing
xni.Current.MoveToParent()
xni.Current.MoveToFirstAttribute()
End While
End
While
Exit
Select
What I'd like to be able to do is only bring back the name only if type = 1 is in the collection of types. I can iterate through all the types in the whole xml sheet but not just for one item.
Hi,
You can express that in XPath:
Dim doc = New XmlDocument() doc.Load("myxml.xml") For Each node As XmlNode In doc.SelectNodes("/items/item[types/type/id=1]/name") Console.WriteLine(node.InnerText) NextThanks,
这篇关于使用XPathNodeIterator循环遍历xml的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!