Solved

Extract data from xml in vb.net

Posted on 2013-06-06
5
418 Views
Last Modified: 2013-08-21
I am new to working with XML and need some help understanding how to cycle through an XML file and extract the data that I need in visual studio . net.  Below is XML containing geocode data that I need to extract.  In this case I need to extract the latitude an longitude tags <lat>33.7713652</lat> <lng>-84.3719365</lng> into variables Lat and Lng.

Thanks in advance for your help.

Here is the xml data:


<?xml version="1.0" encoding="UTF-8"?>
-<GeocodeResponse> <status>OK</status> -<result> <type>street_address</type> <formatted_address>460 North Avenue Northeast, Atlanta, GA 30308, USA</formatted_address> -<address_component> <long_name>460</long_name> <short_name>460</short_name> <type>street_number</type> </address_component> -<address_component> <long_name>North Avenue Northeast</long_name> <short_name>North Avenue NE</short_name> <type>route</type> </address_component> -<address_component> <long_name>Old Fourth Ward</long_name> <short_name>Old Fourth Ward</short_name> <type>neighborhood</type> <type>political</type> </address_component> -<address_component> <long_name>Atlanta</long_name> <short_name>Atlanta</short_name> <type>locality</type> <type>political</type> </address_component> -<address_component> <long_name>Atlanta</long_name> <short_name>Atlanta</short_name> <type>administrative_area_level_3</type> <type>political</type> </address_component> -<address_component> <long_name>Fulton</long_name> <short_name>Fulton</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> -<address_component> <long_name>Georgia</long_name> <short_name>GA</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> -<address_component> <long_name>United States</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> -<address_component> <long_name>30308</long_name> <short_name>30308</short_name> <type>postal_code</type> </address_component> -<geometry> -<location> <lat>33.7713652</lat> <lng>-84.3719365</lng> </location> <location_type>ROOFTOP</location_type> -<viewport> -<southwest> <lat>33.7700162</lat> <lng>-84.3732855</lng> </southwest> -<northeast> <lat>33.7727142</lat> <lng>-84.3705875</lng> </northeast> </viewport> </geometry> </result> -<result> <type>street_address</type> <formatted_address>460 North Road, Douglas, NE 68344, USA</formatted_address> -<address_component> <long_name>460</long_name> <short_name>460</short_name> <type>street_number</type> </address_component> -<address_component> <long_name>North Road</long_name> <short_name>N Rd</short_name> <type>route</type> </address_component> -<address_component> <long_name>Douglas</long_name> <short_name>Douglas</short_name> <type>locality</type> <type>political</type> </address_component> -<address_component> <long_name>Hendricks</long_name> <short_name>Hendricks</short_name> <type>administrative_area_level_3</type> <type>political</type> </address_component> -<address_component> <long_name>Otoe</long_name> <short_name>Otoe</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> -<address_component> <long_name>Nebraska</long_name> <short_name>NE</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> -<address_component> <long_name>United States</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> -<address_component> <long_name>68344</long_name> <short_name>68344</short_name> <type>postal_code</type> </address_component> -<geometry> -<location> <lat>40.5918115</lat> <lng>-96.4191915</lng> </location> <location_type>RANGE_INTERPOLATED</location_type> -<viewport> -<southwest> <lat>40.5904695</lat> <lng>-96.4205405</lng> </southwest> -<northeast> <lat>40.5931675</lat> <lng>-96.4178426</lng> </northeast> </viewport> -<bounds> -<southwest> <lat>40.5918115</lat> <lng>-96.4191916</lng> </southwest> -<northeast> <lat>40.5918255</lat> <lng>-96.4191915</lng> </northeast> </bounds> </geometry> <partial_match>true</partial_match> </result>
0
Comment
Question by:toddpotter
  • 2
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39227206
I see multiple <lat> and <lng> nodes in that sample. So which ones do you need?
0
 

Author Comment

by:toddpotter
ID: 39227225
Multiple get returned so I would go with the 1st occurance.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39227240
OK, but there are multiple parents. Do you need the long/lat that is within the <location> node, the <southeast> node, the <southwest> node, etc., etc.
0
 

Author Comment

by:toddpotter
ID: 39227271
Sorry.  For now, the long/lat within the <location> would be sufficient.
0
 
LVL 13

Accepted Solution

by:
frankhelk earned 500 total points
ID: 39314681
Hmmm - if I copy/paste the code into a file and open it with IE or Firefox (both present XML in nice, readable form) they tell me that the data is not well formed.

That way standard procedures which are built in into the .NET runtime will fail on it.

Nonetheless, here's some code you might use as inspiration. I've stripped down some of my more complicated code, but I havn't tested that afterwards. So this is more a guideline than a working example. The classes involved have much more bells and whistles, so I recommend to have a look at the VS help for 'em.

The VB.NET example (built some years ago with VS2005) fetches some XML text out of a HTTP server and extracts some data. The result is returned as string, with all fetched items sparated by semicolons. But be aware that these classes need well formed xml to work with.

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml

    Public Function GetSthFromXML(ByVal node As String) As String
        Dim xmlDoc As XmlDocument
        Dim xmlnode As XmlNode
        Dim xmlnodeList As XmlNodeList

        Dim url As String
        Dim Response As String
        Dim dWeb As New WebClient

        GetSthFromXML = ""

        xmlDoc = New XmlDocument

        If node <> "" Then

            url = "http://" & node & "/someurl"

            With dWeb
                .Headers = New Net.WebHeaderCollection
                .Encoding = Encoding.Default
                .CachePolicy = New Cache.RequestCachePolicy(Cache.RequestCacheLevel.NoCacheNoStore)
            End With

            Try
                Response = dWeb.DownloadString(url)
            Catch ex As Exception
                'Eror - empty response
                Return ""
            End Try

            xmlDoc.LoadXml(Response)
        Else
            Throw New System.Exception("GetSthFromXML needs a node name")
        End If

        xmlnodeList = xmlDoc.SelectNodes("//NodeType")

        For Each xmlnode In xmlnodeList
            If GetSthFromXML.Length > 0 Then GetSthFromXML = GetSthFromXML & ";"
            GetSthFromXML = GetSthFromXML & xmlnode.SelectSingleNode("InnerNode").InnerText 
        Next

    End Function

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now