2009-09-29 114 views
0

下面是返回的XML的簡化版本。使用VB.NET解析Google Earth XML數據

<?xml version="1.0" encoding="UTF-8" ?> 
    <kml xmlns="http://earth.google.com/kml/2.0"> 
     <Response> 
      <name>1321 herbert street, Warren, MI</name> 
      <Status> 
       <code>200</code> 
       <request>geocode</request> 
      </Status> 
      <Placemark id="p1"> 
       <address>Herbert St, Madison Heights, MI 48071, USA</address> 
      </Placemark>   
      <Placemark id="p2"> 
       <address>Add2</address> 
      </Placemark> 
     </Response> 
    </kml> 

previous文章中,我接到了關於如何將這些數據解析爲對象的幫助。現在。在嘗試讀取重複的PLACEMARK節點時,我需要一點點幫助。

這是對象的結構我想讀成:

Namespace GoogleAddress 

    Public Class kml 

     Private _Response As Response 

     Public Property Response() As Response 
      Get 
       Return _Response 
      End Get 
      Set(ByVal value As Response) 
       _Response = value 
      End Set 
     End Property 

    End Class 

    Public Class Response 

     Private _name As String 
     Public Property name() As String 
      Get 
       Return _name 
      End Get 
      Set(ByVal value As String) 
       _name = value 
      End Set 
     End Property 

     Private _Status As Status 
     Public Property Status() As Status 
      Get 
       Return _Status 
      End Get 
      Set(ByVal value As Status) 
       _Status = value 
      End Set 
     End Property 

     Private _Placemark() As Placemark 
     ' 
     Public Property Placemark() As Placemark() 
      Get 
       Return _Placemark 
      End Get 
      Set(ByVal value As Placemark()) 
       _Placemark = value 
      End Set 
     End Property 

    End Class 


    Public Class Status 

     Private _Code As Integer 
     Public Property Code() As Integer 
      Get 
       Return _Code 
      End Get 
      Set(ByVal value As Integer) 
       _Code = value 
      End Set 
     End Property 

     Private _Request As String 
     Public Property Request() As String 
      Get 
       Return _Request 
      End Get 
      Set(ByVal value As String) 
       _Request = value 
      End Set 
     End Property 

    End Class 

    Public Class Placemark 

     Private _Address As String 
     Public Property Address() As String 
      Get 
       Return _Address 
      End Get 
      Set(ByVal value As String) 
       _Address = value 
      End Set 
     End Property 

    End Class 

End Namespace 

我用下面的常規反序列化上面顯示的XML和填充上述目的:

Public Shared Function DeSerializeFromXMLString(ByVal TypeToDeserialize As System.Type, _ 
                ByVal xmlString As String) As Object 

     Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(xmlString) 
     Dim mem As MemoryStream = New MemoryStream(bytes) 
     Dim ser As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(GoogleAddress.kml), "http://earth.google.com/kml/2.0") 
     Dim KmlResult As GoogleAddress.kml = TryCast(ser.Deserialize(mem), GoogleAddress.kml) ' 

     Return KmlResult 

    End Function 

...但是對象沒有被正確填充,並且我沒有得到任何地標對象(0 len數組)。

任何suggstions?

回答

3

您需要指定一些屬性

Namespace GoogleAddress 
    Public Class kml 
     Private _Response As Response 
     Public Property Response() As Response 
      Get 
       Return _Response 
      End Get 
      Set(ByVal value As Response) 
       _Response = value 
      End Set 
     End Property 
    End Class 

    Public Class Response 
     Private _name As String 
     Public Property name() As String 
      Get 
       Return _name 
      End Get 
      Set(ByVal value As String) 
       _name = value 
      End Set 
     End Property 

     Private _Status As Status 
     Public Property Status() As Status 
      Get 
       Return _Status 
      End Get 
      Set(ByVal value As Status) 
       _Status = value 
      End Set 
     End Property 

     <Xml.Serialization.XmlElement("Placemark")> Public Placemark As Placemark() 
    End Class 
    Public Class Status 
     Private _Code As Integer 
     Public Property Code() As Integer 
      Get 
       Return _Code 
      End Get 
      Set(ByVal value As Integer) 
       _Code = value 
      End Set 
     End Property 

     Private _Request As String 

     Public Property Request() As String 
      Get 
       Return _Request 
      End Get 
      Set(ByVal value As String) 
       _Request = value 
      End Set 
     End Property 
    End Class 

    Public Class Placemark 
     Private _Address As String 
     Private _ID As String 
     <Xml.Serialization.XmlAttribute("ID")> Public Property ID() As String 
      Get 
       Return _ID 
      End Get 
      Set(ByVal value As String) 
       _ID = value 
      End Set 
     End Property 

     Public Property Address() As String 
      Get 
       Return _Address 
      End Get 
      Set(ByVal value As String) 
       _Address = value 
      End Set 
     End Property 
    End Class 
End Namespace 
+0

美麗。謝謝 – ChadD 2009-09-29 12:02:41