使用 VB.NET 解析 Google Earth XML 数据

发布于 2024-08-06 06:25:12 字数 3787 浏览 4 评论 0原文

下面是返回的 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>  

上一篇 发布后,我收到了有关如何将此数据解析为对象的帮助。现在。我需要更多帮助来尝试读取重复的 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

...但是对象没有正确填充,我没有得到任何 Placemark 对象 (0 len大批)。

有什么建议吗?

Here is a simplified version of the XML returned.

<?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>  

In a previous post, I receieved help on how to parse this data into objects. Now. I need a tad bit more help in trying to read in teh repeating PLACEMARK nodes.

This is the object structure I am trying to read into:

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

I use the following routine to deserialize the XML shown above and populate the object above:

   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

...But theobjects are not being properly populated and I do not get any Placemark objects (0 len array).

Any suggstions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-08-13 06:25:12

您需要指定一些属性

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

You need to specify some attributes.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文