Wednesday, November 16, 2011

xml DeSerialization xsd.exe

good article
-------

http://blogs.msdn.com/b/yojoshi/archive/2011/05/14/xml-serialization-and-deserialization-entity-classes-with-xsd-exe.aspx

below is simple steps using VS 2010

1) open XML file that you want to deserialize in VS 2010
2) from VS 2010 menu > XML > Create Schema
3) It will create schema in you project save somewhere this .xsd file (in project)
4) go to VS command prompt
5) change path to your .xsd file (where you save above .xsd file)
6) type : xsd myschemafile.xsd /c (must you /c)
7) it will generate myschemafile.cs file in same location
8) type following code

using (FileStream xmlStream = new FileStream(@"C:\book.xml", FileMode.Open))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(catalog));
catalog deserializedStudents = serializer.Deserialize(xmlReader) as catalog;
foreach (var b in deserializedStudents.book )
{
Console.WriteLine("author : {0}", b.author);
Console.WriteLine("description : {0}", b.description);
Console.WriteLine("genre : {0}", b.genre);
Console.WriteLine("price : {0}", b.price);
Console.WriteLine("");
}
}
}



No comments: