[Help] Get XML Info

Posts 1–14 of 14 · Page 1 of 1
[Help] Get XML Info
Example, I got a webpage in xml with this:

Code:
<ip_address>

  <ip>89.181.133.153</ip>

</ip_address>
How i get the info what is in the <ip> ?
Depends. Is the whole page XML, or only a small section? If the whole page is XML you can create a new XML Document and look for the <ip> node. Otherwise just use regular string manip (GetBetween function in my WebScraping toolkit, advertising ftw!)
Are you only interested in the IP, or all the info?
Quote Originally Posted by Jason View Post
Are you only interested in the IP, or all the info?
All the info but at moment i want to know how to get just that part..

AceKiller Im testing it
Okay wrote a quick demo:

[highlight=vb.net]
Private Function ReadIPAddress() As String
Dim myIp As String = ""
Dim IPreq As Net.HttpWebRequest = CType(Net.WebRequest.Create("http://www.domaintools.com/research/my-ip/myip.xml/"), Net.HttpWebRequest)
Using IPresp As Net.HttpWebResponse = CType(IPreq.GetResponse, Net.HttpWebResponse)
Using XmlRead As New XmlTextReader(IPresp.GetResponseStream)
XmlRead.ReadToFollowing("ip_address")
myIp = XmlRead.ReadContentAsString
End Using
End Using
Return myIp
End Function
[/highlight]
Quote Originally Posted by Jason View Post
Okay wrote a quick demo:

[highlight=vb.netⓘ]
Private Function ReadIPAddress() As String
Dim myIp As String = ""
Dim IPreq As Net.HttpWebRequest = CType(Net.WebRequest.Create("http://www.domaintools.com/research/my-ip/myip.xml/ⓘ"), Net.HttpWebRequest)
Using IPresp As Net.HttpWebResponse = CType(IPreq.GetResponse, Net.HttpWebResponse)
Using XmlRead As New XmlTextReader(IPresp.GetResponseStream)
XmlRead.ReadToFollowing("ip_address")
myIp = XmlRead.ReadContentAsString
End Using
End Using
Return myIp
End Function
[/highlight]
Hey Jason thanks alot , Im using ur code and worked..
But now i want to know how to bypass this.

Im tryin this:

Code:
 Function IP()
        Dim myIP As String = ""
        Dim xmlR As New XmlTextReader(WebBrowser1.Document.Body.InnerText)
        xmlR.ReadToFollowing("ip_adress")
        myIP = xmlR.ReadContentAsString
        Return myIP
    End Function
But i get a error ("Illegal characters in path.") , because of this: "<?xml version="1.0" encoding="utf-8" ?> "

How can i bypass that part? Like : WebBrowser1.Document.Body.InnerText(After the xml declaration) ..

@Jason
Quote Originally Posted by Lyoto Machida View Post


Hey Jason thanks alot , Im using ur code and worked..
But now i want to know how to bypass this.

Im tryin this:

Code:
 Function IP()
        Dim myIP As String = ""
        Dim xmlR As New XmlTextReader(WebBrowser1.Document.Body.InnerText)
        xmlR.ReadToFollowing("ip_adress")
        myIP = xmlR.ReadContentAsString
        Return myIP
    End Function
But i get a error ("Illegal characters in path.") , because of this: "<?xml version="1.0" encoding="utf-8" ?> "

How can i bypass that part? Like : WebBrowser1.Document.Body.InnerText(After the xml declaration) ..

@Jason
Just try
[highlight=vb.net]
Dim myIP As String = ""
Using xmlR As New XmlTextReader(WebBrowser1.DocumentStream)
xmlR.ReadToFollowing("ip_adress")
myIP = xmlR.ReadContentAsString
End Using
Return myIP
[/highlight]

If all you need is the IP, you may as well parse an easier site lol.

Oh yeah, always remember to close and dispose objects like StreamReaders, XmlTextReaders...etc, otherwise the underlying stream will be left open and you'll be wasting memory. For ease of use just use 'Using->End Using'
Quote Originally Posted by Jason View Post


Just try
[highlight=vb.netⓘ]
Dim myIP As String = ""
Using xmlR As New XmlTextReader(WebBrowser1.DocumentStream)
xmlR.ReadToFollowing("ip_adress")
myIP = xmlR.ReadContentAsString
End Using
Return myIP
[/highlight]

If all you need is the IP, you may as well parse an easier site lol.

Oh yeah, always remember to close and dispose objects like StreamReaders, XmlTextReaders...etc, otherwise the underlying stream will be left open and you'll be wasting memory. For ease of use just use 'Using->End Using'
OK Thank you alot, Solved, I used this:

Code:
 Private Function ip()
        Dim myIp As String = ""
        Using XmlRead As New XmlTextReader("http://www.domaintools.com/research/my-ip/myip.xml/")
            XmlRead.ReadToFollowing("ip_address")
            myIp = XmlRead.ReadElementContentAsString
        End Using
        Return myIp.ToString
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        txtIP.Text = ip()
    End Sub
And can i ask you how do you know everything ? xD

Maybe can you post some tuts about XML with vb and that stuff xD

Thanks alot guys.
Bump, I got another question..

Example.. I got this:

<Name>Age</Name>

How do i get like...
For Each Tag in XML text , Get the Name & Age then put it in a textbox like: Name & " , " & Age

Please help me xD
Quote Originally Posted by Lyoto Machida View Post
Bump, I got another question..

Example.. I got this:

<Name>Age</Name>

How do i get like...
For Each Tag in XML text , Get the Name & Age then put it in a textbox like: Name & " , " & Age

Please help me xD
Wait.. what? <Name>Age</Name>

That doesn't sound right. And you should already know how to read to the next named node.
EASY WAY...
[highlight="vb.net"]
Private Function ip()
Dim XML = XElement.Parse(WebClient.DownloadString(YOU WEB SITE))
Return XML...<ip>.value
End Function
[/highlight]
I tried many ways but i only get the first one =/
Posts 1–14 of 14 · Page 1 of 1
This thread is closed for replies.
Talk with us