We help IT Professionals succeed at work.

read an xml file with visual basic .net

chadmanvb
chadmanvb asked
on
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?
Comment
Watch Question

ste5anSenior Developer

Commented:
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

Author

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 Developer

Commented:

Author

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.
Retired
Distinguished Expert 2017
Commented:
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

ste5anSenior Developer
Commented:
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

Author

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 SotoRetired
Distinguished Expert 2017

Commented:
Not a problem chadmanvb, glad to help.