Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a class to generate a sample XML document from XSD schema in .NET

Tags:

.net

xml

xsd

In Visual Studio you can create a template XML document from an existing schema. The new XML Schema Explorer in VS2008 SP1 takes this a stage further and can create a sample XML document complete with data. Is there a class library in .NET to do this automatically without having to use Visual Studio? I found the XmlSampleGenerator article on MSDN but it was written in 2004 so maybe there is something already included in .NET to do this now?

like image 596
Sam Warwick Avatar asked Sep 03 '08 13:09

Sam Warwick


1 Answers

some footwork is involved, but you could load the xsd into a DataSet object, iterate over the Tables and add a few rows in each by calling calling NewRow() on each and then adding those rows back into their respective tables.. then save the DataSet out to a file:

DataSet ds = new DataSet();
ds.ReadXmlSchema("c:/xsdfile.xsd");

foreach(DataTable t in ds.Tables)
{
var row = t.NewRow();
t.Rows.Add(row);
}

ds.WriteXml("c:/example.xml");

P.S. A little extra work, but instead of just iterating over each table type and adding empty rows, you could build a nice winform that would allow you to drop in some data for each of the rows. I built something like this in about an hour a few weeks ago.

like image 107
Andrew Theken Avatar answered Nov 15 '22 06:11

Andrew Theken