Navigate througha XML file?

a8le

Well-known member
Joined
Oct 27, 2005
Messages
75
Programming Experience
5-10
Hi all, what is the best way to navigate through an XML file? The structure of the XML is...


VB.NET:
<?xml version="1.0" standalone="yes" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<words>[INDENT]<aid>1</aid>
<word>one</word>

[/INDENT]</words>

<words>[INDENT]<aid>2</aid>
<word>two</word>

[/INDENT]</words>

<words>[INDENT]<aid>3</aid>
<word>three</word>

[/INDENT]</words>

<words>[INDENT]<aid>4</aid>
<word>four</word>

[/INDENT]</words>

<words>[INDENT]<aid>5</aid>
<word>five</word>

[/INDENT]</words>
</root>

I am loading and reading the document using....

VB.NET:
Dim xmlDocPath As String = MapPath("Words.xml")
Dim xmlReader As XmlTextReader = New XmlTextReader(xmlDocPath)
While (xmlReader.Read())
If xmlReader.NodeType = XmlNodeType.Text Then 
alWordList.Add(xmlReader.Value)
Trace.Write("Added: " & xmlReader.Value)
End If
End While
xmlReader.Close()

As you may see this line works, but with limitations...

VB.NET:
If xmlReader.NodeType = XmlNodeType.Text Then

The problem here is I retrieve all the values of all child nodes.... I don't want the ID numbers "aid" , only the "word" values.

I have
VB.NET:
tried xmlReader.Name = "word"
but that didn't work.

Thanks in advance.
 
Here is one simple way of getting all the words from that xml file.
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmlfilename [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]"test2.xml"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xdoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlDocument
xdoc.Load(xmlfilename)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] allwords [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] items [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNodeList = xdoc.SelectNodes([/SIZE][SIZE=2][COLOR=#800000]"root/words/word"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] item [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] items
allwords += item.InnerText & vbCrLf
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]MsgBox(allwords)
[/SIZE]
 
Back
Top