Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Work with XML?

Posted on 2006-05-25
12
Medium Priority
?
197 Views
Last Modified: 2011-09-20
Need to be able to work with an XML doc that looks like this..

- <Addresses>
- <Address q="202 HERITAGE PARK DR MURFREESBORO TN">
- <Match description="202 HERITAGEPARK DR MURFREESBORO, TN 37129" score="4">
  <StreetNumber>202</StreetNumber>
  <StreetName>HERITAGEPARK</StreetName>
  <StreetType>DR</StreetType>
  <City>MURFREESBORO</City>
  <State>TN</State>
  <ZipCode>37129</ZipCode>
  <Longitude>-86.389653</Longitude>
  <Latitude>35.874850</Latitude>
  </Match>
  </Address>
  </Addresses>

I want the read it and set some values in a class like this..

Public Class Return
    Public MatchDescription As String
    Public Score As Int16
    Public StreetNumber As String
    Public StreetName As String
    Public StreetType As String
    Public City As String
    Public State As String
    Public ZipCode As String
    Public Latitude As Decimal
    Public Longitude As Decimal
End Class

Can anyone help me get started on this?
0
Comment
Question by:NTGuru705
  • 7
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761626
Here is an example, using XmlDocument and XPath expressions:

Imports System.Xml

...

Dim doc As New XmlDocument
doc.Load("c:\temp\test.xml")

For Each match As XmlNode In doc.SelectNodes("//match")
   Dim description As String = match.Attributes("description").Value
   Dim streetNumber As String = match.SelectSingleNode("StreetNumber").InnerText
   ...
Next match

Bob
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16761702
Very close but I want to access at the address level... there may be more than one address record returned.. can you help me with that?  In other words I need to get to each q at the address level and then the matches below that...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761733
Simple adjustment:

Dim doc As New XmlDocument
doc.Load("c:\temp\test.xml")

For Each address As XmlNode In doc.SelectNodes("//address")

   Dim match As XmlNode = address.SelectSingleNode("match")

   Dim description As String = match.Attributes("description").Value
   Dim streetNumber As String = match.SelectSingleNode("StreetNumber").InnerText
   ...
Next address

BTW, the // is the XPath expression for "I don't care where it is in the hierarchy", instead of /addresses/address/match, you could just do //match, as the first post showed.

Here is a good XPath tutorial:
http://www.w3schools.com/xpath/

Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761747
If you have multiple matches then:

Dim doc As New XmlDocument
doc.Load("c:\temp\test.xml")

For Each address As XmlNode In doc.SelectNodes("//address")

   For Each match As XmlNode = address.SelectNodes("match")

     Dim description As String = match.Attributes("description").Value
     Dim streetNumber As String = match.SelectSingleNode("StreetNumber").InnerText
     ...
   Next match

Next address

Bob
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16761750
I think perhaps you meant..

or Each address As XmlNode In doc.SelectNodes("//address")

   Dim match As XmlNode = address.SelectSingleNode("match")

   Dim description As String = match.Attributes("description").Value
   Dim streetNumber As String = match.SelectSingleNode("StreetNumber").InnerText
   ...
Next address <---- CHANGE HERE?
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16761753
bingo I see it now
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16761842
       For Each address As XmlNode In document.SelectNodes("//address")  <-- HERE I GET Variable 'address' hides a variable in an enclosing block.

                For Each match As XmlNode = Address.SelectNodes("match") <-- HERE I GET A SYNTAX ERROR ON THE =
                Response.Query = address.Attributes("q").Value
                Response.MatchDescription = match.Attributes.GetNamedItem("Description").Value
                Response.StreetNumber = match.SelectSingleNode("StreetNumber").InnerText
                Response.StreetName = match.SelectSingleNode("StreetName").InnerText
                Response.StreetType = match.SelectSingleNode("StreetType").InnerText
                Response.City = match.SelectSingleNode("City").InnerText
                Response.State = match.SelectSingleNode("State").InnerText
                Response.ZipCode = match.SelectSingleNode("ZipCode").InnerText
                Response.Longitude = match.SelectSingleNode("Longitude").InnerText
                Response.Latitude = match.SelectSingleNode("Latitude").InnerText
                Response.Add(segment)
            Next
        Next
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16761867
I think this is it..

        Dim Response As New OntokGeoReturn()
        For Each AddressMatch As XmlNode In document.SelectNodes("//Address")
            For Each match As XmlNode In AddressMatch.SelectNodes("Match")
                Response.Query = AddressMatch.Attributes("q").Value
                Response.MatchDescription = match.Attributes.GetNamedItem("Description").Value
                Response.StreetNumber = match.SelectSingleNode("StreetNumber").InnerText
                Response.StreetName = match.SelectSingleNode("StreetName").InnerText
                Response.StreetType = match.SelectSingleNode("StreetType").InnerText
                Response.City = match.SelectSingleNode("City").InnerText
                Response.State = match.SelectSingleNode("State").InnerText
                Response.ZipCode = match.SelectSingleNode("ZipCode").InnerText
                Response.Longitude = match.SelectSingleNode("Longitude").InnerText
                Response.Latitude = match.SelectSingleNode("Latitude").InnerText
            Next
        Next
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16761891
one more question for you.. lets say that I want to only get the first address... even if there are more than one returned?
How would I do this? In other words only read one record.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16761947
Consult the great book of wisdom:
http://www.w3schools.com/xpath/xpath_syntax.asp

<Example>
bookstore/book[1] - Selects the first book element that is the child of the bookstore element
</Example

XPath expression

//address[1]

Bob
0
 
LVL 1

Author Comment

by:NTGuru705
ID: 16765842
OK this is the code for getting one record. Now what to do if the node does not exist?  In other words I cant predict that the node will for sure exist... I get object not set to instance error now.. how to trap?
Thanks

        '=================================================================
        'THIS WILL GET ONLY THE FIRST RECORD
        '=================================================================
        Dim AddressMatch As XmlNode = document.SelectSingleNode("//Address")
        Dim match As XmlNode = AddressMatch.SelectSingleNode("Match[1]")
        GeoCodeReturn.Query = AddressMatch.Attributes("q").Value
        GeoCodeReturn.MatchDescription = match.Attributes("description").Value
        GeoCodeReturn.StreetNumber = match.SelectSingleNode("StreetNumber").InnerText
        GeoCodeReturn.StreetName = match.SelectSingleNode("StreetName").InnerText
        GeoCodeReturn.StreetType = match.SelectSingleNode("StreetType").InnerText
        GeoCodeReturn.City = match.SelectSingleNode("City").InnerText
        GeoCodeReturn.State = match.SelectSingleNode("State").InnerText
        GeoCodeReturn.ZipCode = match.SelectSingleNode("ZipCode").InnerText
        GeoCodeReturn.Longitude = match.SelectSingleNode("Longitude").InnerText
        GeoCodeReturn.Latitude = match.SelectSingleNode("Latitude").InnerText
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16768730
'=================================================================
        'THIS WILL GET ONLY THE FIRST RECORD
        '=================================================================
        Dim AddressMatch As XmlNode = document.SelectSingleNode("//Address")

        If Not AddressMatch Is Nothing Then

          Dim match As XmlNode = AddressMatch.SelectSingleNode("Match[1]")
   
          If Not Match Is Nothing Then
            GeoCodeReturn.Query = AddressMatch.Attributes("q").Value
            GeoCodeReturn.MatchDescription = match.Attributes("description").Value
            GeoCodeReturn.StreetNumber = match.SelectSingleNode("StreetNumber").InnerText
            GeoCodeReturn.StreetName = match.SelectSingleNode("StreetName").InnerText
            GeoCodeReturn.StreetType = match.SelectSingleNode("StreetType").InnerText
            GeoCodeReturn.City = match.SelectSingleNode("City").InnerText
            GeoCodeReturn.State = match.SelectSingleNode("State").InnerText
            GeoCodeReturn.ZipCode = match.SelectSingleNode("ZipCode").InnerText
            GeoCodeReturn.Longitude = match.SelectSingleNode("Longitude").InnerText
            GeoCodeReturn.Latitude = match.SelectSingleNode("Latitude").InnerText
          End If

<End type="Comments">Good Luck!</End>

Bob
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

581 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