Noob هو تحليل بيانات Google Earth XML باستخدام VB.NET، الجزء الثالث

StackOverflow https://stackoverflow.com/questions/1493084

  •  18-09-2019
  •  | 
  •  

سؤال

لدي هذا 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 في كائناتي:

وظيفة المشتركة العامة deserializefromxmString (byval typetodeserialize ك system.type، _ byval xmlstring كسلسلة) ككائن

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

وظيفة النهاية

المشكلة هي عندما أحاول تحليل عقدة الأدوات الترابية. شيء عن السمات هو شد الأشياء وأنا لا أعرف كيفية التعامل معها. إذا قمت بإزالة معلومات السمة على العقد التجارية، فإن التحيز يعمل بشكل جيد، لكن ليس لدي هذا الخيار.

قد تلاحظ أن محاولتي تعليقي للتعامل مع سمة الدقة مثل تم القيام به لسمة المعرف السابقة على عقدة موضع العلامة.

ماذا علي أن أفعله للحصول عليه للعمل مع معلومات السمة على العقد التجارية؟

هل كانت مفيدة؟

المحلول

جرب هذا:

<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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top