Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / XML

Serializing Nested XML

Rate me:
Please Sign up or sign in to vote.
1.33/5 (2 votes)
2 Nov 2007CPOL 51K   272   9   6
Serializing and deserializing nested XML.

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:

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>

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:

VB
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSerializing Nested XML - List Element Variable Initialization Pin
VEGALOZ4-Oct-10 4:24
VEGALOZ4-Oct-10 4:24 
GeneralCodeXS XSD-to-Code Generator Tool Pin
Willem Fourie8-Nov-07 7:04
Willem Fourie8-Nov-07 7:04 
GeneralXsd.exe alternative Pin
BoneSoft5-Nov-07 8:27
BoneSoft5-Nov-07 8:27 
AnswerRe: Xsd.exe alternative Pin
Rekhender6-Nov-07 8:21
Rekhender6-Nov-07 8:21 
Generalxsd.exe Pin
Steven Campbell2-Nov-07 10:07
Steven Campbell2-Nov-07 10:07 
<br />
C:\Projects>xsd testxml.xml<br />
Microsoft (R) Xml Schemas/DataTypes support utility<br />
[Microsoft (R) .NET Framework, Version 2.0.50727.42]<br />
Copyright (C) Microsoft Corporation. All rights reserved.<br />
Writing file 'C:\Projects\testxml.xsd'.<br />


Then...

C:\Projects>xsd testxml.xsd /classes<br />
Microsoft (R) Xml Schemas/DataTypes support utility<br />
[Microsoft (R) .NET Framework, Version 2.0.50727.42]<br />
Copyright (C) Microsoft Corporation. All rights reserved.<br />
Writing file 'C:\Projects\testxml.cs'.<br />



GeneralRe: xsd.exe Pin
Rekhender3-Nov-07 15:31
Rekhender3-Nov-07 15:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.