VB.NET: Extract element and attribute using ChildNode method

toytoy

Well-known member
Joined
Jul 16, 2004
Messages
46
Programming Experience
1-3
Say I have these Xml tag:
VB.NET:
<hi help=”example”>
	<how>
	 <to>do</to>
	 <or> 
		<give suggestion=”on” doing=”it”/>
	 </or>
	 <thank>bye</thank>
	</how>
	<how>
		…
	</how>
</hi>
I want to use the childNode method to do for example:
VB.NET:
Dim xdoc As XmlDocument
Dim nodelist As XmlNodeList
Dim node As XmlNode
Dim to, thank, suggestion, doing As String
xdoc = New XmlDocument
 
xdoc.LoadXml("example.xml")
 
nodelist = xdoc.SelectNodes("/hi/how")
 
For Each node In nodelist
  to = node.ChildNodes.Item(0).InnerText
  thank = node.ChildNodes.Item(2).InnerText
  listbox.Items.Add( to & thank)
  Dim giveNode As XmlNode = node.ChildNodes.Item(1).ChildNodes.Item(0)
  suggestion = giveNode.Attributes(0).Value
  doing = giveNode.Attributes(1).Value
  listbox.Items.Add( suggestion & doing)
Next
I can use the above code to get the content of to, thank, suggestion and doing...but how to get back the same content if i change the Xml tag to:
VB.NET:
<hi help=”example”>
	<how to=”do”>
	 <give suggestion=”on” doing=”it”/> 
	 <thank>bye</thank>
	</how>
	<how>
		…
	</how>
</hi>
Thanks
 
Last edited:
I manage to extract only the "to" and "thank" tag with the following code:
VB.NET:
to = node.Attributes.GetNamedItem("to").Value
thank = node.ChildNodes.Item(1).InnerText
Can anybody help me to get the suggestion and doing attributes...

Thanks
 
Back
Top