Solved

Error reading certain lines of an XML

Posted on 2009-05-06
12
193 Views
Last Modified: 2013-11-26
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>

Open in new window

0
Comment
Question by:zerogeek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24316748
Please include the xml data that is failing.
0
 

Author Comment

by:zerogeek
ID: 24316796
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
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24316852
Your nodes don't match..

<text>Another Test</test>

text and test. typo :]
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zerogeek
ID: 24316903
Actually, I wrote that up really quick. It is an example of  the XML I am using.
0
 

Author Comment

by:zerogeek
ID: 24316925
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
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24317133
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

Open in new window

0
 

Author Comment

by:zerogeek
ID: 24317256
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>

Open in new window

0
 

Author Comment

by:zerogeek
ID: 24317273
Their are multiple <status> tags in 1 XML file between the <statuses type="array"> and </statuses> tags
0
 
LVL 8

Accepted Solution

by:
RyanAndres earned 250 total points
ID: 24317310

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

Open in new window

0
 

Author Comment

by:zerogeek
ID: 24317371
That did not work. It still only grabs the text from the first tag.
0
 

Author Comment

by:zerogeek
ID: 24317419
Nevermind. I didn't notice the change from XMLDoc.SelectSingleNode to SelectNodes

Thanks for all your help. :)
0
 

Author Closing Comment

by:zerogeek
ID: 31578522
Great help in a short amount of time. Much appreciated.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question