문제

다음은 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

... 그러나 TheObjects는 제대로 채워지지 않으며 Placemark 객체 (0 Len Array)를 얻지 못합니다.

제안이 있습니까?

도움이 되었습니까?

해결책

일부를 지정해야합니다 속성.

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top