Answered Reading XML from external server?

Joined
Oct 8, 2011
Messages
7
Location
Indonesia
Programming Experience
Beginner
I'm trying to get the number in the tag called <num> from here and Label1.Text that number.

I tried this and it doesn't work, maybe I am making a mistake or this code is outdated?
Dim doc as XPathDocument
Dim nav as XPathNavigator
Dim iter as XPathNodeIterator

doc = New XPathDocument("http://apps.mta-tr.com/ASE/server.mta.vg:22003.xml")
nav = doc.CreateNavigator
iter = nav.Select("/MTA/num")
While iter.MoveNext
Dim iterNews As XPathNodeIterator
lstNav = iter.Current
iterNews = lstNav.SelectDescendants(XPathNodeType.Element, False)
While iterNews.MoveNext
Label1.Text = iterNews.Current.Name & ": " & iterNews.Current.Value
End While
End While


I've spend hours trying to get this work.
 
Last edited:
help said:
The XPathNodeIterator object is positioned on the first node in the selected node set only after the initial call to the MoveNext method.
You only need to call MoveNext once to move to the first MTA/num result. Using SelectSingleNode is simpler and also more appropriate since you only are selecting a single node.
or this code is outdated?
It is not outdated, but current XDocument class is more modern and simpler. An example:
Dim doc = XDocument.Load(url)
Dim value = doc.<MTA>.<num>.Value
' or as descendant
value = doc...<num>.Value
 
You only need to call MoveNext once to move to the first MTA/num result. Using SelectSingleNode is simpler and also more appropriate since you only are selecting a single node.

It is not outdated, but current XDocument class is more modern and simpler. An example:
Dim doc = XDocument.Load(url)
Dim value = doc.<MTA>.<num>.Value
' or as descendant
value = doc...<num>.Value

I guess you're right, I'm really blown away that 3 lines of code could do it, thanks JohnH. Really appreciate it. And yeah it works, amazing.
 
I guess you're right, I'm really blown away that 3 lines of code could do it, thanks JohnH. Really appreciate it. And yeah it works, amazing.
Two lines actually, and your code would also only be two lines if you did what I suggested and also compacted unnecessary code lines:
Dim doc As New XPathDocument(url)
Dim num = doc.CreateNavigator.SelectSingleNode("/MTA/num").Value
 
Two lines actually, and your code would also only be two lines if you did what I suggested and also compacted unnecessary code lines:
Dim doc As New XPathDocument(url)
Dim num = doc.CreateNavigator.SelectSingleNode("/MTA/num").Value

If I do that I get the message:

'CreateNavigator' is not a member of 'System.Xml.Linq.XDocument'.
 
That code can't possibly produce that error, doc variable is declared as XPathDocument.
 
That code can't possibly produce that error, doc variable is declared as XPathDocument.

Oh yes, you're right. I forgot to set doc As New. Thanks again!

Edit: Instead of making a new thread maybe I should put it in here since its almost same subject.

Now after being able to get informations from the .xml document I need to list the <players> tag, aligned properly like how it shows there.

        Dim doc = XDocument.Load("http://apps.mta-tr.com/ASE/server.mta.vg:22003.xml")
        Dim value = doc.<MTA>.<players>.Value
        value = doc...<players>.Value


        RichTextBox1.Text = value



Did that but I get it all in 1 line without space. Quite hard to explain it.
 
Last edited:
Back
Top