Wednesday, October 19, 2011

XSD to XML Conversion Code

Converting a XSD to XML file.....using ADO.net way.....


private void ReadSchemaFromXmlTextReader(string filename){
// Create the DataSet to read the schema into.
DataSet thisDataSet = new DataSet();
// Create a FileStream object with the file path and name.
System.IO.FileStream myFileStream = new System.IO.FileStream
(filename,System.IO.FileMode.Open);
// Create a new XmlTextReader object with the FileStream.
System.Xml.XmlTextReader myXmlTextReader=
new System.Xml.XmlTextReader(myFileStream);
// Read the schema into the DataSet and close the reader.
thisDataSet.ReadXmlSchema(myXmlTextReader);
myXmlTextReader.Close();
}


private void WriteXmlToFile(DataSet thisDataSet) {
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "myXmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream myFileStream = new System.IO.FileStream
(filename, System.IO.FileMode.Create);
// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter myXmlWriter =
new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(myXmlWriter);
myXmlWriter.Close();
}

Cheers!!!

No comments: