tags:

views:

1150

answers:

2

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?

A: 

Directly, none that I can think of, other than third party add-ons. You could utilize the xsd schema definition tool to take your XSD and create a .NET object/class, once you have that, you could, to quote the linked page:

XSD to Classes: Generates runtime classes from an XSD schema file. The generated classes can be used in conjunction with System.Xml.Serialization.XmlSerializer to read and write XML code that follows the schema.

curtisk
+2  A: 

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.

Andrew Theken