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
Sometimes, you might have found some schema files which you wanted to manipulate in your code with .NET. Microsoft provides a sample XSD to C#/VB.NET converter as a download to do this task yourself. Please at the following details to download it and use it.
== FROM Microsoft ==
Overview
The Sample Code Generator (XSDObjectGen) tool takes an XSD schema as input and generates sample code showing how to mark up C# and VB.Net classes so that when serialized with the XML serializer, the resulting XML will be valid according to the original schema. This update fixes some documentation changes and corrects a problem where the wizard did not generate code in some environments.
Link: Download

