Error reading certain lines of an XML

I am having an issue reading a node from certain XML files. Below is the code I use to read most XML files that my app needs. However this code seems to have an issue reading a certain file. I believe it is because of:
<statuses type="array">

Is there a way to read this XML file?
Dim xml As Object
Dim XMLDoc As New XmlDocument
Dim nodeList As XmlNodeList
Dim node As XmlNode

XMLDoc.Load("c:\temp\update.xml")

nodeList = XMLDoc.SelectSingleNode("/status/user").ChildNodes
For i As Integer = 0 To nodeList.Count - 2 Step 2
node = nodeList(i)
Dim user As String = node.InnerText
MessageBox.show(user)
Next

'Reads this XML:

<?xml version="1.0" encoding="UTF-8"?>
<status>
<user>Testing</user>
</status>

'However the code below will not read the XML below.

Dim xml As Object
Dim XMLDoc As New XmlDocument
Dim nodeList As XmlNodeList
Dim node As XmlNode

XMLDoc.Load("c:\temp\times.xml")

nodeList = XMLDoc.SelectSingleNode("statuses/status/text").ChildNodes
For i As Integer = 0 To nodeList.Count - 2 Step 2
node = nodeList(i)
Dim text As String = node.InnerText
MessageBox.show(text)
Next

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<text>Another Test</test>
</status>
</statuses>

Asked:
Who is Participating?

Commented:

Dim XMLDoc As New XmlDocument()
Dim nodeList As XmlNodeList

XMLDoc.Load("file.txt")
nodeList = XMLDoc.SelectNodes("statuses/status/text")

For Each node As XmlNode In nodeList
Dim text As String = node.InnerText
Next

0

Commented:
Please include the xml data that is failing.
0

Author Commented:
It is listed under the code I am using to read the XML:

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<text>Another Test</test>
</status>
</statuses>

It doesn't fail. It runs thru the code but never shows the messagebox.
MessageBox.show(text)
0

Commented:
Your nodes don't match..

<text>Another Test</test>

text and test. typo :]
0

Author Commented:
Actually, I wrote that up really quick. It is an example of  the XML I am using.
0

Author Commented:
I believe it has something to do with the code that reads the XML file. It seems to be getting hung up on the <statuses type="array"> and either skipping the entire XML or not getting deep enough into the nodes to read the innerText of <text>Another Test</text>
0

Commented:
The problem was your for loop.

The SelectSingleNode found the xpath just fine.

See below for changes.
Dim xml As Object = Nothing
Dim XMLDoc As New XmlDocument()
Dim nodeList As XmlNodeList
Dim node As XmlNode

XMLDoc.Load("file.txt")

nodeList = XMLDoc.SelectSingleNode("statuses/status/text").ChildNodes
For i As Integer = 0 To nodeList.Count - 1
node = nodeList(i)
Dim text As String = node.InnerText
MessageBox.show(text)
Next

0

Author Commented:
That seems to be working. Is there a way to make this grab up to 10 more from the file? Example of XML below
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<text>Testing</text>
</status>
<status>
<text>Number 2</text>
</status>
<status>
<text>And so on...</text>
</status>

0

Author Commented:
Their are multiple <status> tags in 1 XML file between the <statuses type="array"> and </statuses> tags
0

Author Commented:
That did not work. It still only grabs the text from the first tag.
0

Author Commented:
Nevermind. I didn't notice the change from XMLDoc.SelectSingleNode to SelectNodes

Thanks for all your help. :)
0

Author Commented:
Great help in a short amount of time. Much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Already a member? Login.

All Courses

From novice to tech pro — start learning today.