VB.NET中将XML解析为对象的最简单方法是什么?

时间:2009-09-29 05:08:47

标签: xml vb.net web-services

VB.NET解析此XML的最简单方法是什么?

以下是完整来源的示例:

View Source XML

我相信XML可以直接读入与XML结构相匹配的类结构。

让我们从上面的例子中获取一些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>X</Status> 
  </Response>
  </kml>

我以为我可以使用这个代码,从另一个SO帖子中获取,将XML转换为相应的对象

Imports System.IO
Imports System.Text

Public Class Xml

    Public Shared Function SerializeToXMLString(ByVal ObjectToSerialize As Object) As String
        Dim mem As MemoryStream = New MemoryStream()
        Dim ser As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType())
        ser.Serialize(mem, ObjectToSerialize)
        Dim ascii As ASCIIEncoding = New ASCIIEncoding()
        Return ascii.GetString(mem.ToArray())
    End Function

    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(TypeToDeserialize)
        Return ser.Deserialize(mem)
    End Function

End Class

..但问题是,我如何定义这些对象 - 即使是缩小的简单示例?

例如,kml部分令人困惑..

我刚开始上课......

Public Class kml

    Public Class Response


        Public Name As String
        Public Status As String

    End Class

End Class

..但是我运行这段代码:

Dim kml As kml
kml = CType(Xml.DeSerializeFromXMLString(GetType(kml), XmlDoc.OuterXml), kml)

..我收到解析错误。

我也试过这个:

Public Class kml

    Private _Response As New Response

    Public ReadOnly Property Response() As Response
        Get
            Return _Response
        End Get
    End Property

End Class

Public Class Response


    Public name As String
    Public Status As String

End Class

我知道我可能会离开这里。有人请把我推向正确的方向......

THX

跟进问题。

如何处理嵌套集合,例如,此示例中的地标集合?

2 个答案:

答案 0 :(得分:3)

您的kmlResponse类应如下所示:

  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 String
        Public Property Status() As String
            Get
                Return _Status
            End Get
            Set(ByVal value As String)
                _Status = value
            End Set
        End Property
    End Class

然后你可以像这样反序列化:

Dim serializer As New XmlSerializer(GetType(kml), "http://earth.google.com/kml/2.0")               
Dim result As kml = TryCast(serializer.Deserialize(mem), kml)

答案 1 :(得分:1)

摆脱Class Response,然后使用GetType(kml)

基本上,您不需要嵌套类(尽管您可以使用它们)。嵌套元素表示为属性,其名称对应于元素名称;如果属性本身是类类型,它可以有更多嵌套元素(依次映射到该类的属性)。类本身名称很重要的唯一情况是根元素。