VB.NET反序列化XML



序列化,是将对象状态转换为可保持或传输的格式的过程。

与序列化相反的是反序列化,它将流转换为对象。这两个过程结合起来,就可以存储和传输数据。这就是序列化的意义所在。


在VB.NET中转换、处理和生成XML文档时,需要用到一些XML专用名称空间中的类,这些名称空间包括:

System.Xml    该名称空间提供了对各种XML标准(包括DTD、名称空间、DOM、XDR(XML Data Reduced,XML架构标准的旧版本)、XPath、XSLT和SOAP(以前表示Simple Object Access Protocol标准,现在什么也不表示))的核心支持。

System.Xml.Serialization    该名称空间提供的对象使用序列化技术进行对象与XML文档或流之间的转换。

System.Xml.Schema    该名称空间提供一组用于加载、创建和输出架构的对象,这些对象可以在内存中操作组成XML架构的各种实体

System.Xml.Xpath    该名称空间为XPath(XML Path Language)提供语法分析程序和估算引擎。

System.Xml.Xsl    该名称空间提空了使用XSL(Extensible Stylesheet Language)和XSLT(XSL Transformation)时必须用到的对象。

System.Xml.Linq    该名称空间提供了使用LINQ查询XML的支持。

今天我想讨论的是VB.NET反序列化XML。

以下是我想反序列化的XML的结构:

  1. <card>  
  2.     <sCodeBorder y1='34' y2='120' x2='568' x1='360' />  
  3.     <border y1='0' y2='1008' x2='1650' x1='0' />  
  4.     <area>  
  5.         <question id='1' x1='70' y1='274' x2='180' y2='288' >  
  6.             <option id='A' x1='70' y1='274' x2='90' y2='288' />  
  7.         </question>  
  8.         <question id='2' x1='70' y1='298' x2='180' y2='312' >  
  9.             <option id='A' x1='70' y1='298' x2='90' y2='312' />  
  10.             <option id='B' x1='100' y1='298' x2='120' y2='312' />  
  11.         </question>  
  12.         <question id='3' x1='70' y1='322' x2='180' y2='336' >  
  13.             <option id='A' x1='70' y1='322' x2='90' y2='336' />  
  14.             <option id='B' x1='100' y1='322' x2='120' y2='336' />  
  15.             <option id='C' x1='130' y1='322' x2='150' y2='336' />  
  16.         </question>  
  17.     </area>  
  18. </card>  
<card>
	<sCodeBorder y1='34' y2='120' x2='568' x1='360' />
	<border y1='0' y2='1008' x2='1650' x1='0' />
	<area>
		<question id='1' x1='70' y1='274' x2='180' y2='288' >
			<option id='A' x1='70' y1='274' x2='90' y2='288' />
		</question>
		<question id='2' x1='70' y1='298' x2='180' y2='312' >
			<option id='A' x1='70' y1='298' x2='90' y2='312' />
			<option id='B' x1='100' y1='298' x2='120' y2='312' />
		</question>
		<question id='3' x1='70' y1='322' x2='180' y2='336' >
			<option id='A' x1='70' y1='322' x2='90' y2='336' />
			<option id='B' x1='100' y1='322' x2='120' y2='336' />
			<option id='C' x1='130' y1='322' x2='150' y2='336' />
		</question>
	</area>
</card>

我们首先要做的是创建与XML相对应的对象,然后再把XML转换成我们想要的对象。

创建的类中需要引用System.Xml.Serialization 命名空间。这一命名空间包含用于将对象序列化为XML格式文档或流的类。

  1. Public Class card  
  2.     <XmlElementAttribute("sCodeBorder")> Public sCodeBorder As sCodeBorder  
  3.     <XmlElementAttribute("border")> Public border As border  
  4.     <XmlElementAttribute("area")> Public area As area  
  5.   
  6.     Public Sub New()  
  7.     End Sub  
  8.   
  9.     Public Sub New(ByVal sCodeBorder As sCodeBorder, ByVal border As border, ByVal area As area )  
  10.         Me.sCodeBorder = sCodeBorder  
  11.         Me.border = border  
  12.         Me.area = area  
  13.     End Sub  
  14. End Class  
Public Class card
    <XmlElementAttribute("sCodeBorder")> Public sCodeBorder As sCodeBorder
    <XmlElementAttribute("border")> Public border As border
    <XmlElementAttribute("area")> Public area As area

    Public Sub New()
    End Sub

    Public Sub New(ByVal sCodeBorder As sCodeBorder, ByVal border As border, ByVal area As area )
        Me.sCodeBorder = sCodeBorder
        Me.border = border
        Me.area = area
    End Sub
End Class

  1. Public Class sCodeBorder  
  2.     <XmlAttributeAttribute("x1")> Public x1 As Integer  
  3.     <XmlAttributeAttribute("x2")> Public x2 As Integer  
  4.     <XmlAttributeAttribute("y1")> Public y1 As Integer  
  5.     <XmlAttributeAttribute("y2")> Public y2 As Integer  
  6.   
  7.     Public Sub New()  
  8.     End Sub  
  9.     Public Sub New(ByVal x1 As IntegerByVal x2 As IntegerByVal y1 As IntegerByVal y2 As Integer)  
  10.         Me.x1 = x1  
  11.         Me.x2 = x2  
  12.         Me.y1 = y1  
  13.         Me.y2 = y2  
  14.     End Sub  
  15. End Class  
Public Class sCodeBorder
    <XmlAttributeAttribute("x1")> Public x1 As Integer
    <XmlAttributeAttribute("x2")> Public x2 As Integer
    <XmlAttributeAttribute("y1")> Public y1 As Integer
    <XmlAttributeAttribute("y2")> Public y2 As Integer

    Public Sub New()
    End Sub
    Public Sub New(ByVal x1 As Integer, ByVal x2 As Integer, ByVal y1 As Integer, ByVal y2 As Integer)
        Me.x1 = x1
        Me.x2 = x2
        Me.y1 = y1
        Me.y2 = y2
    End Sub
End Class

  1. Public Class border  
  2.     <XmlAttributeAttribute("x1")> Public x1 As Integer  
  3.     <XmlAttributeAttribute("x2")> Public x2 As Integer  
  4.     <XmlAttributeAttribute("y1")> Public y1 As Integer  
  5.     <XmlAttributeAttribute("y2")> Public y2 As Integer  
  6.   
  7.     Public Sub New()  
  8.     End Sub  
  9.     Public Sub New(ByVal x1 As IntegerByVal x2 As IntegerByVal y1 As IntegerByVal y2 As Integer)  
  10.         Me.x1 = x1  
  11.         Me.x2 = x2  
  12.         Me.y1 = y1  
  13.         Me.y2 = y2  
  14.     End Sub  
  15. End Class  
Public Class border
    <XmlAttributeAttribute("x1")> Public x1 As Integer
    <XmlAttributeAttribute("x2")> Public x2 As Integer
    <XmlAttributeAttribute("y1")> Public y1 As Integer
    <XmlAttributeAttribute("y2")> Public y2 As Integer

    Public Sub New()
    End Sub
    Public Sub New(ByVal x1 As Integer, ByVal x2 As Integer, ByVal y1 As Integer, ByVal y2 As Integer)
        Me.x1 = x1
        Me.x2 = x2
        Me.y1 = y1
        Me.y2 = y2
    End Sub
End Class

  1. Public Class area  
  2.     <XmlElementAttribute("question")> Public Ques_List() As question  
  3.     Public Sub New()  
  4.     End Sub  
  5.   
  6.     Public Sub New(ByVal multiQ() As question)  
  7.         Me.Ques_List = multiQ  
  8.     End Sub  
  9. End Class  
Public Class area
    <XmlElementAttribute("question")> Public Ques_List() As question
    Public Sub New()
    End Sub

    Public Sub New(ByVal multiQ() As question)
        Me.Ques_List = multiQ
    End Sub
End Class


question类中维护了option类的对象集合。
  1. Public Class question  
  2.     <XmlAttributeAttribute("x1")> Public x1 As Integer  
  3.     <XmlAttributeAttribute("x2")> Public x2 As Integer  
  4.     <XmlAttributeAttribute("y1")> Public y1 As Integer  
  5.     <XmlAttributeAttribute("y2")> Public y2 As Integer  
  6.     <XmlAttributeAttribute("id")> Public id As String  
  7.     <XmlElementAttribute("option")> Public multiOptions_List() As option_  
  8.     Public Sub New()  
  9.     End Sub  
  10.   
  11.     Public Sub New(ByVal x1 As IntegerByVal x2 As IntegerByVal y2 As IntegerByVal y1 As IntegerByVal id As StringByVal multiO() As option_)  
  12.         Me.x1 = x1  
  13.         Me.x2 = x2  
  14.         Me.y2 = y2  
  15.         Me.y1 = y1  
  16.         Me.id = id  
  17.         Me.multiOptions_List = multiO  
  18.     End Sub  
  19. End Class  
Public Class question
    <XmlAttributeAttribute("x1")> Public x1 As Integer
    <XmlAttributeAttribute("x2")> Public x2 As Integer
    <XmlAttributeAttribute("y1")> Public y1 As Integer
    <XmlAttributeAttribute("y2")> Public y2 As Integer
    <XmlAttributeAttribute("id")> Public id As String
    <XmlElementAttribute("option")> Public multiOptions_List() As option_
    Public Sub New()
    End Sub

    Public Sub New(ByVal x1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal y1 As Integer, ByVal id As String, ByVal multiO() As option_)
        Me.x1 = x1
        Me.x2 = x2
        Me.y2 = y2
        Me.y1 = y1
        Me.id = id
        Me.multiOptions_List = multiO
    End Sub
End Class

  1. Public Class option_  
  2.     <XmlAttributeAttribute("x1")> Public x1 As Integer  
  3.     <XmlAttributeAttribute("x2")> Public x2 As Integer  
  4.     <XmlAttributeAttribute("y1")> Public y1 As Integer  
  5.     <XmlAttributeAttribute("y2")> Public y2 As Integer  
  6.     <XmlAttributeAttribute("id")> Public id As String  
  7.   
  8.     Public Sub New()  
  9.     End Sub  
  10.   
  11.     Public Sub New(ByVal x1 As IntegerByVal x2 As IntegerByVal y2 As IntegerByVal y1 As IntegerByVal id As String)  
  12.         Me.x1 = x1  
  13.         Me.x2 = x2  
  14.         Me.y2 = y2  
  15.         Me.y1 = y1  
  16.         Me.id = id  
  17.     End Sub  
  18. End Class  
Public Class option_
    <XmlAttributeAttribute("x1")> Public x1 As Integer
    <XmlAttributeAttribute("x2")> Public x2 As Integer
    <XmlAttributeAttribute("y1")> Public y1 As Integer
    <XmlAttributeAttribute("y2")> Public y2 As Integer
    <XmlAttributeAttribute("id")> Public id As String

    Public Sub New()
    End Sub

    Public Sub New(ByVal x1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal y1 As Integer, ByVal id As String)
        Me.x1 = x1
        Me.x2 = x2
        Me.y2 = y2
        Me.y1 = y1
        Me.id = id
    End Sub
End Class

这样XML要反序列化的类就建立好了。虽然说有些复杂,但是有了这些类,我们就不用一个一个地处理XML的节点了~

具体到反序列化的代码,就很简单了。代码如下:

  1. Dim cardXML As FileStream = New FileStream(xmlPath, FileMode.Open)  
  2. 'card是类名,也是根节点  
  3. Dim serialize As XmlSerializer = New XmlSerializer(GetType(card))  
  4. Dim wholeCard As card = serialize.Deserialize(cardXML)  
  5. cardXML.Close()  
Dim cardXML As FileStream = New FileStream(xmlPath, FileMode.Open)
'card是类名,也是根节点
Dim serialize As XmlSerializer = New XmlSerializer(GetType(card))
Dim wholeCard As card = serialize.Deserialize(cardXML)
cardXML.Close()

上面代码的xml来源是磁盘上的文件。

但有的时候,xml是以字符串的形式给出的。这时候我们该如何处理呢?

从反序列化的定义可知,反序列化是把流转为对象。

上面的代码是以文件流的形式来反序列化。如果是字符串的话,我们就需要把字符串存入内存流中,再把内存流反序列化。

  1. Dim descBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(xmlString)  
  2. Dim serialize As XmlSerializer = New XmlSerializer(GetType(card))  
  3. Dim wholeCard As card = serialize.Deserialize(New MemoryStream(descBytes))  
Dim descBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(xmlString)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(card))
Dim wholeCard As card = serialize.Deserialize(New MemoryStream(descBytes))

以上就是xml反序列化的过程。


参考文献:Visual.Basic.2010 & NET 4 高级编程



转载于:http://blog.csdn.net/u013162930/article/details/47975881

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值