Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Practical XML Usage  »  First Steps to building the application
 »  Home  »  Data Programming  »  XML  »  Practical XML Usage  »  First Steps to building the application
Practical XML Usage
by Gaidar Magdanurov | Published  08/20/2006 | .NET Newbie XML | Rating:
First Steps to building the application

Step by step guide to building the desired application

Step 1. Create a schema for our discography file.
It is not a difficult job to create a schema for such a simple XML file but for more complex files the process of authoring a schema can take a long time. But with Visual Studio you don't have to worry! Open an XML file in Visual Studio and click on the XML > Create Schema menu item.

Then you see the schema automatically generated for the XML file. Right-click on the code and select View Designer to visually design the schema. In our particular situation we don't have to change anything and can just save the schema file for further use. The schema file is presented below.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="discography">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="album">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="year" type="xs:int" />
                            <xs:element name="label" type="xs:string" />
                            <xs:element name="songs">
                                <xs:complexType>
                                    <xs:sequence>
                                            <xs:element maxOccurs="unbounded" name="song" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="band" type="xs:string" use="required" />
        <xs:attribute name="firstYear" type="xs:int" use="required" />
        <xs:attribute name="lastYear" type="xs:int" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>

Step 2. Create a class corresponding to XML file
Now we have to create a class file to have an ability to serialize/deserialize objects to and from XML files. For sure, we can do some coding but I prefer to use tools to generate class files automatically. To do this we should go to command-line and use xsd.exe utility which is available in .NET Framework SDK. You can use shortcut SDK Command Prompt from Microsoft .NET Framework SDK folder in Start menu. In SDK Command Prompt type:

xsd.exe /c /l:vb path_to_schema_file

Option /c indicates that we want xsd.exe to generate a serializable class file and not a typed data-set file.  Option /l:vb tells the utility to generate Visual Basic class file (to create a C# file use /l:cs option). By default, the generated class file will be placed to the same directory in which the schema file resides.

The listing of the enerated class file is the following.

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization

<SYSTEM.CODEDOM.COMPILER.GENERATEDCODEATTRIBUTE("XSD", _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _
Partial Public Class discography

    Private albumField() As discographyAlbum
    Private bandField As String
    Private firstYearField As Integer
    Private lastYearField As Integer

    <SYSTEM.XML.SERIALIZATION.XMLELEMENTATTRIBUTE("ALBUM")>_
    Public Property album() As discographyAlbum()
        Get
            Return Me.albumField
        End Get
        Set
            Me.albumField = value
        End Set
    End Property

    <SYSTEM.XML.SERIALIZATION.XMLATTRIBUTEATTRIBUTE()>_
    Public Property band() As String
        Get
            Return Me.bandField
        End Get
        Set
            Me.bandField = value
        End Set
    End Property

    <SYSTEM.XML.SERIALIZATION.XMLATTRIBUTEATTRIBUTE()>_
    Public Property firstYear() As Integer
        Get
            Return Me.firstYearField
        End Get
        Set
            Me.firstYearField = value
        End Set
    End Property

    <SYSTEM.XML.SERIALIZATION.XMLATTRIBUTEATTRIBUTE()>_
    Public Property lastYear() As Integer
        Get
            Return Me.lastYearField
        End Get
        Set
            Me.lastYearField = value
        End Set
    End Property
End Class


<SYSTEM.CODEDOM.COMPILER.GENERATEDCODEATTRIBUTE("XSD", _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true)> _
Partial Public Class discographyAlbum

    Private titleField As String
    Private yearField As Integer
    Private labelField As String
    Private songsField() As String

    Public Property title() As String
        Get
            Return Me.titleField
        End Get
        Set
            Me.titleField = value
        End Set
End Property

    Public Property year() As Integer
        Get
            Return Me.yearField
        End Get
        Set
            Me.yearField = value
        End Set
    End Property

    Public Property label() As String
        Get
            Return Me.labelField
        End Get
        Set
            Me.labelField = value
        End Set
    End Property

    <SYSTEM.XML.SERIALIZATION.XMLARRAYITEMATTRIBUTE("SONG", IsNullable:="false)">_
    Public Property songs() As String()
        Get
            Return Me.songsField
        End Get
        Set
            Me.songsField = value
        End Set
    End Property
End Class

Sponsored Links