read an xml file with visual basic .net

I have never worked with XML before with .net.  I set a small xml file to a variable called strIPInfo that looks like.

<?xml version="1.0" encoding="UTF-8"?>

                                    <response>

                                          <query_status>

                                                <query_status_code>OK</query_status_code>

                                                <query_status_description>Query successfully performed.</query_status_description>

                                          </query_status>

                                          <server_data>
<status>ok</status>
<ip>122.222.222.193</ip>
<country>US</country>
<region>Missouri</region>
<city>Saint Louis</city>
<postalcode>63333</postalcode>
<isp>My ISP</isp>
<time>-06:00</time>
<latitude>31.627269744873</latitude>
<longitude>-93.197891235352</longitude>
</server_data>

                                    </response>

How can I read that variable and get info like "isp", "ip", "city" ect?  Can i do this all with out haveing to write the xml to a file?
chadmanvbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
E.g.

    Sub Main()

        Dim xElement As XElement =
            <response>
                <query_status>
                    <query_status_code>OK</query_status_code>
                    <query_status_description>Query successfully performed.</query_status_description>
                </query_status>
                <server_data>
                    <status>ok</status>
                    <ip>122.222.222.193</ip>
                    <country>US</country>
                    <region>Missouri</region>
                    <city>Saint Louis</city>
                    <postalcode>63333</postalcode>
                    <isp>My ISP</isp>
                    <time>-06:00</time>
                    <latitude>31.627269744873</latitude>
                    <longitude>-93.197891235352</longitude>
                </server_data>
            </response>

        Console.WriteLine(xElement.Element("server_data").Element("isp").Value)
        Console.WriteLine(xElement.Descendants("isp").FirstOrDefault().Value)
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

Open in new window

chadmanvbAuthor Commented:
ste5an, Can I do this by reading my variable call strIPInfo?  So currently I have the xml file as a string called strIPInfo
ste5anSenior DeveloperCommented:
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

chadmanvbAuthor Commented:
I'm not sure how that will work.  I cant figure out how to get it to read my variable that is in the form of a string.
Fernando SotoRetiredCommented:
Hi chadmanvb

Try something like this.

'' This is the XML you placed into a string variable
'' Test XML
Dim strLPInfo As String = _
     "<?xml version=""1.0"" encoding=""utf-8""?>" & _
      "<response>" & _
      "  <query_status>" & _
      "    <query_status_code>OK</query_status_code>" & _
      "  <query_status_description>Query successfully performed.</query_status_description>" & _
      "  </query_status>" & _
      "  <server_data>" & _
      "    <status>ok</status>" & _
      "    <ip>122.222.222.193</ip>" & _
      "    <country>US</country>" & _
      "    <region>Missouri</region>" & _
      "    <city>Saint Louis</city>" & _
      "    <postalcode>63333</postalcode>" & _
      "    <isp>My ISP</isp>" & _
      "    <time>-06:00</time>" & _
      "    <latitude>31.627269744873</latitude>" & _
      "    <longitude>-93.197891235352</longitude>" & _
      "  </server_data>" & _
      "</response>"

'' This statment loads an XML in a string variable into a XML
'' Document. If the XML document is on the file system you can
'' load it directly by using this statement which is commentted out
'' Dim xdoc As XDocument = XDocument.Load("Path & File Name as a String")
Dim xdoc As XDocument = XDocument.Parse(strLPInfo)
'' Get a reference to the section of the XML you want data from
Dim info As XElement = xdoc.Root.Element("server_data")

'' Get the data and place them into variables
Dim isp As String = info.Element("isp").Value
Dim ip As String = info.Element("ip").Value
Dim city As String = info.Element("city").Value

'' In order to get the others is the server_data node do the following
'' Note that XML is case sensitive even in Visual Basic. Therefore a
'' node name like city above is not the same as City.
Dim GiveVariableName As String = info.Element("NodeName Here").Value

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Really?

    Sub Main()

        Dim content As String =
            "<response>" & _
            "    <query_status>" & _
            "        <query_status_code>OK</query_status_code>" & _
            "        <query_status_description>Query successfully performed.</query_status_description>" & _
            "    </query_status>" & _
            "    <server_data>" & _
            "        <status>ok</status>" & _
            "        <ip>122.222.222.193</ip>" & _
            "        <country>US</country>" & _
            "        <region>Missouri</region>" & _
            "        <city>Saint Louis</city>" & _
            "        <postalcode>63333</postalcode>" & _
            "        <isp>My ISP</isp>" & _
            "        <time>-06:00</time>" & _
            "        <latitude>31.627269744873</latitude>" & _
            "        <longitude>-93.197891235352</longitude>" & _
            "    </server_data>" & _
            "</response>"

        Dim xElement As XElement = xElement.Parse(content)

        Console.WriteLine(xElement.Element("server_data").Element("isp").Value)
        Console.WriteLine(xElement.Descendants("isp").FirstOrDefault().Value)
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

Open in new window

chadmanvbAuthor Commented:
Thanks so much guys!   Ste5an, think you had the right idea to begin with, but I was to dumb to get it working.  Thanks again to both of you!  I have it working.
Fernando SotoRetiredCommented:
Not a problem chadmanvb, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.