首页 > 代码库 > VB.NET反序列化XML

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的结构:

<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格式文档或流的类。

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

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

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

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类的对象集合。


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

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的节点了~

详细到反序列化的代码。就非常easy了。代码例如以下:

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是以字符串的形式给出的。

这时候我们该怎样处理呢?

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

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

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反序列化的过程。


xml序列化:

‘去掉xml声明
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.OmitXmlDeclaration = True
settings.Encoding = Encoding.Default
Dim mem As System.IO.MemoryStream = New MemoryStream()
Using writer As XmlWriter = XmlWriter.Create(mem, settings)

	‘去除默认命名空间xmlns:xsd和xmlns:xsi
	Dim nss As XmlSerializerNamespaces = New XmlSerializerNamespaces()
	nss.Add("", "")
	Dim formatter As XmlSerializer = New XmlSerializer(wholeCardA.GetType())
	formatter.Serialize(writer, wholeCardA, nss)
End Using
Dim resultStr as string = Encoding.Default.GetString(mem.ToArray())

该方法实现了序列化xml对象及去掉xml声明和默认命名空间。



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

VB.NET反序列化XML