Monday, June 22, 2009

Prepare XML from XSD using xml serilization.

Here we will learn how do we create a class from XSD file can prepare a XML from


Suppose I have an XML like this.



<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<country>USA</country>

<company>Columbia</company>



<price>10.90</price>

<year>1985</year>

</cd>

<cd>

<title>Hide your heart</title>

<artist>Bonnie Dylan</artist>

<country>UK</country>



<company>CBS Records</company>

<price>9.90</price>

<year>1988</year>

</cd>

<cd>

<title>Greatest Hits</title>

<artist>Bob Parton</artist>



<country>USA</country>

<company>RCA</company>

<price>9.90</price>

<year>1982</year>

</cd>

</cds>



Now using the xsd.exe or any third party tool u can create a class from a XSd. Now
to use xsd.exe u must have schema file of an XML. So first i have generated a xsd
from above xml.. (One can have a ready made xsd too, its not always i have to prepare
the xsd from given xml.. okay!! ) well so i have prepared an xsd from it..which



<?xml version="1.0" encoding="UTF-8" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementformdefault="qualified">

<xs:element name="cds">

<xs:complextype>

<xs:sequence>

<xs:element maxoccurs="unbounded" ref="cd" />

</xs:sequence>

</xs:complextype>

</xs:element>

<xs:element name="cd">

<xs:complextype>

<xs:sequence>

<xs:element ref="title" />

<xs:element ref="artist" />

<xs:element ref="country" />

<xs:element ref="company" />

<xs:element ref="price" />

<xs:element ref="year" />

</xs:sequence>

</xs:complextype>

</xs:element>

<xs:element name="title" type="xs:string" />

<xs:element name="artist" type="xs:string" />

<xs:element name="country" type="xs:NCName" />

<xs:element name="company" type="xs:string" />

<xs:element name="price" type="xs:decimal" />

<xs:element name="year" type="xs:integer" />

</xs:schema>


Now using the xsd.exe / XsdObjectGenerator.exe (U can find such utilities by googling
out ) I have prepared a class file from it. This class has been generated using
xsd object generator.. using the following command XSDObjectGen cd.xsd /l:cs /n:myNameSpace
/f:cdClass




// Copyright 2004, Microsoft Corporation

// Sample Code - Use restricted to terms of use defined in the accompanying license
agreement (EULA.doc)



//--------------------------------------------------------------

// Autogenerated by XSDObjectGen version 1.4.2.1

// Schema file: cd.xsd

// Creation Date: 22-Jun-09 12:17:28 PM

// Created by Kavita Khandhadia

//--------------------------------------------------------------



using System;

using System.Xml.Serialization;

using System.Collections;

using System.Xml.Schema;

using System.ComponentModel;


public struct Declarations

{

public const string SchemaVersion = "";

}





[Serializable]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public class cdCollection : ArrayList

{

public cd Add(cd obj)

{

base.Add(obj);

return obj;

}



public cd Add()

{

return Add(new cd());

}



public void Insert(int index, cd obj)

{

base.Insert(index, obj);

}



public void Remove(cd obj)

{

base.Remove(obj);

}



new public cd this[int index]

{

get { return (cd) base[index];
}

set { base[index] = value;
}

}

}







[XmlRoot(ElementName="cds",IsNullable=false),Serializable]

public class cds

{

[System.Runtime.InteropServices.DispIdAttribute(-4)]

public IEnumerator GetEnumerator()


{

return cdCollection.GetEnumerator();

}



public cd Add(cd obj)

{

return cdCollection.Add(obj);

}



[XmlIgnore]

public cd this[int index]

{

get { return (cd) cdCollection[index];
}

}



[XmlIgnore]

public int Count


{

get { return
cdCollection.Count; }

}



public void Clear()

{

cdCollection.Clear();

}



public cd Remove(int index)


{


cd obj = cdCollection[index];

cdCollection.Remove(obj);

return obj;

}



public void Remove(object obj)

{

cdCollection.Remove(obj);

}



[XmlElement(Type=typeof(cd),ElementName="cd",IsNullable=false,Form=XmlSchemaForm.Qualified)]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public cdCollection __cdCollection;




[XmlIgnore]

public cdCollection cdCollection

{

get

{

if (__cdCollection
== null) __cdCollection = new cdCollection();

return
__cdCollection;

}

set {__cdCollection = value;}

}



public cds()

{

}

}





[XmlRoot(ElementName="cd",IsNullable=false),Serializable]

public class cd

{



[XmlElement(ElementName="title",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public string __title;




[XmlIgnore]

public string title

{


get { return __title; }

set { __title = value;
}

}



[XmlElement(ElementName="artist",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public string __artist;




[XmlIgnore]

public string artist

{


get { return __artist;
}

set { __artist = value;
}

}



[XmlElement(ElementName="country",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="NCName")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public string __country;




[XmlIgnore]

public string country

{


get { return __country;
}

set { __country = value;
}

}



[XmlElement(ElementName="company",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="string")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public string __company;




[XmlIgnore]

public string company

{


get { return __company;
}

set { __company = value;
}

}



[XmlElement(ElementName="price",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="decimal")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public decimal __price;




[XmlIgnore]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public bool __priceSpecified;




[XmlIgnore]

public decimal price

{


get { return __price; }

set { __price = value;
__priceSpecified = true; }

}



[XmlElement(ElementName="year",IsNullable=false,Form=XmlSchemaForm.Qualified,DataType="integer")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public string __year;




[XmlIgnore]

public string year

{


get { return __year; }

set { __year = value; }

}



public cd()

{

}

}


Now in the code behind I have written this logic



public void CallCDSxsd()

{

cds cdRoot = new cds();

cdCollection cdColl = new cdCollection();

cd oneCdNode = new cd();

oneCdNode.title = "Room Service";

oneCdNode.artist = "Bryan Adams";

oneCdNode.company = "MMM";

oneCdNode.country = "USA";

oneCdNode.price = 455M;

oneCdNode.year = "2004";

cdRoot.Add(oneCdNode);


oneCdNode = new cd();

oneCdNode.title = "Laundry Service";

oneCdNode.artist = "Sakira";

oneCdNode.company = "MMM";

oneCdNode.country = "USA";

oneCdNode.price = 355M;

oneCdNode.year = "2005";

cdRoot.Add(oneCdNode);


XmlElement xe = Serialize(cdRoot);


}

/// <summary>

/// Serialize given object into XmlElement.

/// </summary>

/// <param name="transformObject">Input object
for serialization.</param>

/// <returns>Returns serialized XmlElement.</returns>

#region Serialize given object into stream.

public XmlElement Serialize(object transformObject)

{

XmlElement serializedElement = null;

try

{

MemoryStream
memStream = new MemoryStream();

XmlSerializer
serializer = new XmlSerializer(transformObject.GetType());

serializer.Serialize(memStream,
transformObject);

memStream.Position
= 0;

XmlDocument xmlDoc
= new XmlDocument();

xmlDoc.Load(memStream);

serializedElement
= xmlDoc.DocumentElement;

}

catch (Exception SerializeException)

{




}

return serializedElement;

}

#endregion // End - Serialize given object into stream.




Now when u will look at this property xe.OuterXml U will see out put as following.


<cds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<cd>

<title>Room Service</title>

<artist>Bryan Adams</artist>

<country>USA</country>

<company>MMM</company>

<price>455</price>

<year>2004</year>

</cd>

<cd>

<title>Laundry Service</title>

<artist>Sakira</artist>

<country>USA</country>

<company>MMM</company>

<price>355</price>

<year>2005</year>

</cd>

</cds>

1 comment: