嵌套 XML 的序列化
序列化和反序列化嵌套 XML。
引言
就在几天前,我的同事在反序列化带有某些值的嵌套 XML 文件时遇到了问题。我们在互联网上搜索,但没有找到任何与该问题相关的良好解决方案。所以最后,我编写了这个代码,当然,在我的同事的帮助下。 看起来代码运行良好,但我希望其他人提供改进它的建议。
背景
如上所述,我在反序列化带有值和属性的嵌套 XML 时遇到了问题。 示例 XML 如下
<?xml version="1.0" encoding="UTF-8" ?>
<article id="97536" status="new">
<analysis type="ALERT">
<content>
<header>
<date>20071010</date>
<hour>14:56</hour>
<author>God</author>
<media type="IMAGE"></media>
<code type="001">Hello</code>
<code type="002"></code>
<code type="TICK">MM</code>
<country>India</country>
<name>Wipro</name>
<product>WIPS</product>
<option>
<period_unit>day</period_unit>
<period_unit_count>1</period_unit_count>
<trend type="0020">0</trend>
<trend type="0050">0</trend>
<trend type="0020_50">0</trend>
<trend type="0101_SL">0</trend>
<trend type="0909_0">0</trend>
<volatility type="LITERS">1</volatility>
<momentum type="11170">0</momentum>
<momentum type="11130">0</momentum>
<strength type="VOLUME">0</strength>
</option>
</header>
</content>
</analysis>
</article>
使用代码
该代码已分解为几个类,具体取决于 XML 中嵌套项的数量。 因此,对于上面的示例 XML,我们需要定义尽可能多的类,最顶层的是“Article”。
类定义如下
Imports System.Xml
<Serializable()> _
Public Class article
<Xml.Serialization.XmlAttributeAttribute("id")> _
Public id As String = "9000"
<Xml.Serialization.XmlAttributeAttribute("status")> _
Public status As String = "old"
<Xml.Serialization.XmlElement("analysis")> _
Public analysis As List(Of analysis)
Public Sub New()
analysis = New List(Of analysis)
End Sub
End Class
Public Class analysis
<Xml.Serialization.XmlAttributeAttribute("type")> _
Public type As String = "ALERT"
<Xml.Serialization.XmlElement("content")> _
Public content As List(Of content)
Public Sub New()
content = New List(Of content)
End Sub
End Class
Public Class content
<Xml.Serialization.XmlElement("header")> _
Public header As List(Of header)
Public Sub New()
header = New List(Of header)
End Sub
End Class
Public Class header
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public [date] As String = "10000"
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public hour As String = "hour"
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public author As String = "Author"
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public country As String = "India"
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public name As String = "Name"
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public product As String = "USD"
<System.Xml.Serialization.XmlElementAttribute("media", _
GetType(media), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _media As List(Of media)
<System.Xml.Serialization.XmlElementAttribute("code", _
GetType(code), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _code As List(Of code)
<System.Xml.Serialization.XmlElementAttribute("option", _
GetType([option]), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _option As List(Of [option])
Public Sub New()
_media = New List(Of media)
_code = New List(Of code)
_option = New List(Of [option])
End Sub
End Class
Public Class media
Private _type As String = "IMAGE"
Private _value As String = "ZERO"
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
Public Class code
Private _type As String = "ISIN"
Private _value As String = "FR0000121220"
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
Public Class [option]
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public period_unit As String = "day"
<System.Xml.Serialization.XmlElementAttribute(
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public period_unit_count As String = "1"
<System.Xml.Serialization.XmlElementAttribute("trend", _
GetType(trend), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _trend As List(Of trend)
<System.Xml.Serialization.XmlElementAttribute("volatility", _
GetType(volatility), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _volatility As List(Of volatility)
<System.Xml.Serialization.XmlElementAttribute("momentum", _
GetType(momentum), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _momentum As List(Of momentum)
<System.Xml.Serialization.XmlElementAttribute("strength", _
GetType(strength), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public _strength As List(Of strength)
Public Sub New()
_trend = New List(Of trend)
_volatility = New List(Of volatility)
_momentum = New List(Of momentum)
_strength = New List(Of strength)
End Sub
End Class
Public Class trend
Private _type As String = "MM20"
Private _value As String = "1"
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
Public Class volatility
Private _type As String = "BOLLINGER"
Private _value As String = "1"
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
Public Class momentum
Private _type As String = "RS0190"
Private _value As String = "0"
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
Public Class strength
Private _type As String = "VOLUME"
Private _value As String = "0"
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property type() As String
Get
Return _type
End Get
Set(ByVal value As String)
_type = value
End Set
End Property
<System.Xml.Serialization.XmlTextAttribute()> _
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
End Class
代码不言自明。 我提供了用于测试的默认值,但您可以删除它们。