locked
Get data from XML document. RRS feed

  • Question

  • I am new to XML, but I would like to get some of the data from this XML document:

    http://pastebin.com/S7eUNmL2

    Using this code:

            Dim Document As New XmlDocument
            Document.LoadXml(xml)
            Dim DocumentElement As XmlElement = Document.DocumentElement
            Dim ResourceSets As XmlNode = DocumentElement.ChildNodes.ItemOf(6)
            Dim ResourceSet As XmlNode = ResourceSets.ChildNodes(0)
            Dim Resource As XmlNode = ResourceSet.ChildNodes(1)
            Dim LocationList As XmlNodeList = Resource.ChildNodes
            Dim Location As XmlNode = LocationList.ItemOf(0)
            Dim Name As String = Location.SelectSingleNode("Name").Value

    But I get an Object reference not set to an instance of an object. exception on the last line of code. If I quickwatch the Location value it is the correct node, but I'm at a loss of what to do...

     

    • Edited by ecnepsnai Thursday, January 26, 2012 1:35 AM Better question
    Wednesday, January 25, 2012 9:53 PM

Answers

  • Hello ian-spence,

    Try the following which does several of the items where looking to get. From here if this works for you then adapt to adding items to your class as shown prior to editing your original post.

    Imports <xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
    Public Class Form4
        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Doc As New XDocument
            Doc = XDocument.Load("MyLocations.xml")
    
            Dim query = _
            ( _
                From T In Doc...<Location> _
                Select _
                Name = T.<Name>.Value, _
                Latitude = T.<Point>.<Latitude>.Value, _
                Longitude = T.<Point>.<Longitude>.Value, _
                CountryRegion = T.<Address>.<CountryRegion>.Value _
            ).ToList
    
    
            For Each item In query
                Console.WriteLine("[{0}] [{1}] [{2}] [{3}]", _
                                  item.Name, _
                                  item.Latitude, _
                                  item.Longitude, _
                                  item.CountryRegion _
            )
            Next
        End Sub
    End Class
    


    Results

    [Perth, Australia] [-31.953020095825195] [115.85723876953125] [Australia]
    [Perth, Perth and Kinross, United Kingdom] [56.396049499511719] [-3.4324100017547607] [United Kingdom]

     


    KSG
    • Proposed as answer by Mark Liu-lxf Monday, January 30, 2012 6:50 AM
    • Marked as answer by Mark Liu-lxf Friday, February 3, 2012 7:30 AM
    Thursday, January 26, 2012 1:51 AM