Noob 正在使用 VB.NET 解析 Google Earth XML 数据,第 III 部分

发布于 2024-08-06 07:33:05 字数 5529 浏览 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>
      <AddressDetails Accuracy="6" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
        <Country>
          <CountryNameCode>US</CountryNameCode>
          <CountryName>USA</CountryName>
        </Country>
      </AddressDetails>
    </Placemark>
    <Placemark id="p2">
      <address>Herbert Ave, Warren, MI 48089, USA</address>
      <AddressDetails Accuracy="6" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
        <Country>
          <CountryNameCode>US</CountryNameCode>
          <CountryName>USA</CountryName>
        </Country>
      </AddressDetails>
    </Placemark>
  </Response>
</kml>

这是我的对象:

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

        <XmlElementAttribute("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 _AddressDetails As AddressDetails
        Private _ID As String

        <XmlAttributeAttribute("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

        '<XmlAttributeAttribute("Accuracy")> _
        Public Property AddressDetails() As AddressDetails
            Get
                Return _AddressDetails
            End Get
            Set(ByVal value As AddressDetails)
                _AddressDetails = value
            End Set
        End Property

    End Class

    Public Class AddressDetails

        Private _Country As Country
        Public Property Country() As Country
            Get
                Return _Country
            End Get
            Set(ByVal value As Country)
                _Country = value
            End Set
        End Property

    End Class

    Public Class Country

        Private _CountryNameCode As String
        Public Property CountryNameCode() As String
            Get
                Return _CountryNameCode
            End Get
            Set(ByVal value As String)
                _CountryNameCode = value
            End Set
        End Property

        Private _CountryName As String
        Public Property CountryName() As String
            Get
                Return _CountryName
            End Get
            Set(ByVal value As String)
                _CountryName = 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

问题是当我尝试解析 AddressDetails 节点时。关于属性的一些事情把事情搞砸了,我不知道如何处理。如果我删除 AddressDetails 节点上的属性信息,反序列化工作正常,但我没有该选项。

您可能会注意到我注释掉了处理 Accuracy 属性的尝试,就像对 Placemark 节点上的早期 ID 属性所做的那样。

我需要做什么才能让它与 AddressDetail 节点上的属性信息一起工作?

I have this XML that I am trying to read into an object hierarchy:

<?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>
      <AddressDetails Accuracy="6" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
        <Country>
          <CountryNameCode>US</CountryNameCode>
          <CountryName>USA</CountryName>
        </Country>
      </AddressDetails>
    </Placemark>
    <Placemark id="p2">
      <address>Herbert Ave, Warren, MI 48089, USA</address>
      <AddressDetails Accuracy="6" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
        <Country>
          <CountryNameCode>US</CountryNameCode>
          <CountryName>USA</CountryName>
        </Country>
      </AddressDetails>
    </Placemark>
  </Response>
</kml>

Here are my objects:

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

        <XmlElementAttribute("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 _AddressDetails As AddressDetails
        Private _ID As String

        <XmlAttributeAttribute("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

        '<XmlAttributeAttribute("Accuracy")> _
        Public Property AddressDetails() As AddressDetails
            Get
                Return _AddressDetails
            End Get
            Set(ByVal value As AddressDetails)
                _AddressDetails = value
            End Set
        End Property

    End Class

    Public Class AddressDetails

        Private _Country As Country
        Public Property Country() As Country
            Get
                Return _Country
            End Get
            Set(ByVal value As Country)
                _Country = value
            End Set
        End Property

    End Class

    Public Class Country

        Private _CountryNameCode As String
        Public Property CountryNameCode() As String
            Get
                Return _CountryNameCode
            End Get
            Set(ByVal value As String)
                _CountryNameCode = value
            End Set
        End Property

        Private _CountryName As String
        Public Property CountryName() As String
            Get
                Return _CountryName
            End Get
            Set(ByVal value As String)
                _CountryName = value
            End Set
        End Property

    End Class
End Namespace

...and I am using this routine to deserialize the XML into my objects:

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

The problem is when I try parse the AddressDetails node. Something about the Attributes is screwing things up and I don't know how to handle it. If I remove the attribute information on the AddressDetails nodes, the deserialization works fine, but I don't have that option.

You may notice my commented out attempt to handle Accuracy attribute like was done for the earlier ID attribute on the Placemark node.

What do I have to do to get it to work with the attribute info on the AddressDetail nodes?

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

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

发布评论

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

评论(1

迷迭香的记忆 2024-08-13 07:33:05

试试这个:

<XmlElement(elementName:="AddressDetails", Namespace:="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0")> Public Property AddressDetails() As AddressDetails
    Get
        Return _AddressDetails
    End Get
    Set(ByVal value As AddressDetails)
        _AddressDetails = value
    End Set
End Property



    <XmlAttributeAttribute("Accuracy")> Public Property Accuracy() As Integer
        Get
            Return _Accuracy
        End Get
        Set(ByVal value As Integer)
            _Accuracy = value
        End Set
    End Property

Try this:

<XmlElement(elementName:="AddressDetails", Namespace:="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0")> Public Property AddressDetails() As AddressDetails
    Get
        Return _AddressDetails
    End Get
    Set(ByVal value As AddressDetails)
        _AddressDetails = value
    End Set
End Property



    <XmlAttributeAttribute("Accuracy")> Public Property Accuracy() As Integer
        Get
            Return _Accuracy
        End Get
        Set(ByVal value As Integer)
            _Accuracy = value
        End Set
    End Property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文