XML反序列化为自定义对象列表

时间:2017-11-25 18:31:50

标签: xml vb.net serialization deserialization

我正在尝试反序列化来自webrequest的XML字符串,但我无法将其添加到我可以迭代的自定义对象列表中,我只花了2个小时左右,我就可以& #39;弄清楚我做错了什么,这是我第一次尝试类似的东西,所以此时我有点无能为力。

所以这是我尝试反序列化的XML:

<?xml version="1.0" encoding="UTF-8"?>
<school_search>
  <summary>
    <total_schools>5</total_schools>
    <category>private</category>
  </summary>
  <schools>
    <school>
      <school_id>12</school_id>
      <school_name>School of Literature</school_name>
    </school>
    <school>
      <school_id>31</school_id>
      <school_name>School of Sports</school_name>
    </school>
    <school>
      <school_id>38</school_id>
      <school_name>School of Arts</school_name>
    </school> 
    <school>
      <school_id>40</school_id>
      <school_name>School of Science</school_name>
    </school>
    <school>
      <school_id>43</school_id>
      <school_name>School of Business</school_name>
    </school>
  </schools>
</school_search>

这是我为处理此特定XML而创建的类:

<XmlRoot(ElementName:="school_search")>
    Public Class xmlSchool_search
        <XmlElement(ElementName:="summary")>
        Public Property Summary() As xmlSummary
            Get
                Return m_Summary
            End Get
            Set
                m_Summary = Value
            End Set
        End Property
        Private m_Summary As xmlSummary
        <XmlElement(ElementName:="schools")>
        Public Property Schools() As xmlSchools
            Get
                Return m_Schools
            End Get
            Set
                m_Schools = Value
            End Set
        End Property
        Private m_Schools As xmlSchools
    End Class

    <XmlRoot(ElementName:="schools")>
    Public Class xmlSchools
        <XmlElement(ElementName:="school")>
        Public Property School() As List(Of xmlSchool)
            Get
                Return m_School
            End Get
            Set
                m_School = Value
            End Set
        End Property
        Private m_School As List(Of xmlSchool)
    End Class

    <XmlRoot(ElementName:="summary")>
    Public Class xmlSummary
        <XmlElement(ElementName:="total_Schools")>
        Public Property Total_schools() As String
            Get
                Return m_Total_schools
            End Get
            Set
                m_Total_schools = Value
            End Set
        End Property
        Private m_Total_schools As String

        <XmlElement(ElementName:="category")>
        Public Property Category() As String
            Get
                Return m_Category
            End Get
            Set
                m_Category = Value
            End Set
        End Property
        Private m_Category As String

    End Class

    <XmlRoot(ElementName:="school")>
    Public Class xmlSchool
        <XmlElement(ElementName:="school_id")>
        Public Property School_id() As String
            Get
                Return m_School_id
            End Get
            Set
                m_School_id = Value
            End Set
        End Property
        Private m_School_id As String

        <XmlElement(ElementName:="school_name")>
        Public Property School_name() As String
            Get
                Return m_School_name
            End Get
            Set
                m_School_name = Value
            End Set
        End Property
        Private m_School_name As String

    End Class

这是将xml反序列化为自定义类的相关代码:

   request = DirectCast(WebRequest.Create(address), HttpWebRequest)
   response = DirectCast(request.GetResponse(), HttpWebResponse)

   Dim schoolsList As List(Of xmlSchool)
   Using reader As New StreamReader(response.GetResponseStream())
         Dim deserializer As New XmlSerializer(GetType(List(Of xmlSchool)), New XmlRootAttribute("schools"))
         schoolsList = DirectCast(deserializer.Deserialize(reader), List(Of xmlSchool))
   End Using

最后这是我用来迭代它的东西:

 For Each school As xmlSchool In schoolsList
    HttpContext.Current.Response.Write(school.School_id)
    HttpContext.Current.Response.Write("<br/>")
 Next

问题: 我总是得到以下例外:

System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: <school_search xmlns=''> was not expected.

我试图将XmlRootAttribute更改为school_search并且它没有抛出异常,但是schoolList是空的,我相信问题在于自定义类,但我只是看不到问题在哪里。

先谢谢你们花时间研究这个问题。

2 个答案:

答案 0 :(得分:1)

您不需要xmlSchools课程,您可以在List<of xmlSchool>课程中将学校集合作为xmlSchool_search类型的财产引入。
然后使用XmlArray属性告诉序列化程序当前属性表示集合。

<XmlRoot(ElementName:="school_search")>
Public Class xmlSchool_search
    <XmlElement(ElementName:="summary")>
    Public Property Summary As xmlSummary

    <XmlArray(ElementName:="schools")>
    Public Property Schools As List(Of xmlSchool)
End Class

<XmlType(ElementName:="summary")>
Public Class xmlSummary
    <XmlElement(ElementName:="total_schools")>
    Public Property Total_schools As String

    <XmlElement(ElementName:="category")>
    Public Property Category As String
End Class

<XmlType(ElementName:="school")>
Public Class xmlSchool
    <XmlElement(ElementName:="school_id")>
    Public Property School_id As String

    <XmlElement(ElementName:="school_name")>
    Public Property School_name As String
End Class

反序列化

Dim deserializer As New XmlSerializer(GetType(xmlSchool_search))
Dim search As xmlSchool_search = Nothing

Using reader As New StreamReader(response.GetResponseStream())
     search = DirectCast(deserializer.Deserialize(reader), xmlSchool_search )
End Using

Dim schools As List(Of xmlSchool) = search.Schools

因为属性设置器和getter中没有自定义逻辑,所以可以使用属性简写

Public Property Name As String

答案 1 :(得分:1)

我喜欢使用xml linq解析简单的xml

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()

        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim search As Search = doc.Descendants("school_search").Select(Function(x) New Search() With { _
                                                                           .total_schools = x.Descendants("total_schools").FirstOrDefault(), _
                                                                           .category = x.Descendants("category").FirstOrDefault(), _
                                                                           .schools = x.Descendants("school").Select(Function(y) New School With { _
                                                                                                                         .name = y.Element("school_name"), _
                                                                                                                         .id = y.Element("school_id") _
                                                                                                                                         }).ToList()
                                                                       }).FirstOrDefault()
    End Sub

End Module
Public Class Search
    Public total_schools As Integer
    Public category As String
    Public schools As List(Of School)
End Class
Public Class School
    Public name As String
    Public id As Integer
End Class