I was curiout about this tool in Microsoft which can generate classes to serialize/deserialize XSD based xml files back and forth. I installed the tool provided by microsoft in my previous column. Created a small project with customers/customer xml snippet and tested it. It was simple and worked great.
To read existing xml generated for xsd:
private void ReadExistingFile(ref FileStream fStream, ref MemoryStream mStream)
{
fStream = new FileStream(”XMLFile1.xml”, FileMode.Open);
byte[] byteArray = new byte[90000];
mStream = new MemoryStream(byteArray);
// root xsd class
XmlSerializer s = new XmlSerializer(typeof(customers));
customers allcustomers = (customers)s.Deserialize(fStream);
foreach (customer c in allcustomers)
{
textBox1.Text += c.id + “\r\n”;
}
}
Also to create a new snippet:
private static FileStream WriteANewXMLForThisXSDGeneratedClass(FileStream fStream2)
{
// write something new
fStream2 = new FileStream(”XMLFile2.xml”, FileMode.Create);
customers tobeWritten = new customers();
customer muthu = new customer();
muthu.id = “one”;
muthu.name = “muthu”;
customer kumar = new customer();
kumar.id = “two”;
kumar.name = “kumar”;
tobeWritten.Add(muthu);
tobeWritten.Add(kumar);
XmlSerializer s = new XmlSerializer(typeof(customers));
s.Serialize(fStream2, tobeWritten);
return fStream2;
}
It’s really cool…..
- Download the entire solution from here: Sample
