Introduction
Just a few days back, my colleague had to face a problem when de-serializing a nested XML file which had attributes with some values. We searched on the internet but were not able to find any good solution relating to the problem. So finally, I created this code, of course, with the help of my colleague. Seems that the code works fine, but I would like input from others on how to improve it.
Background
As explained above, I was having a problem deserializing nested XML which had attributes along with values. The sample XML was like this:
="1.0"="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>
Using the code
The code has been broken into several classes depending on how much nested items we have in our XML. So for the sample XML above, we need to define as many classes with the top most being "Article".
The classes are defined as shown:
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
The code is self-explanatory. I have given default values for testing, but you can remove those.