The following code is for quick and dirty command line program that generates an XML instance from an XML-serializable .NET class. It uses BizTalk 2004's instance generation capabilities, so if you happen to find that useful, this code allows you to use it with .NET classes. For example, let's say you have a complex configuration class ServiceConfiguration in assembly c:\whatever\TheSystem.Configuration.dll, and you compile the code below to btsi.exe, the following command will generate a sample instance:
btsi.exe “c:\whatever\TheSystem.Configuration.dll” ServiceConfiguration “c:\whatever\ServiceConfigurationSample.xml”
Here's the code. Just paste it into a console app's default class, add a reference to:
{BizTalkInstallPath}\Developer Tools\Microsoft.BizTalk.TOM.dll
and build.
It's attrociously commented, but then again, painlessly simple:
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
using Microsoft.BizTalk.TOM;
namespace btsi
{
///
/// Console application that generates a sample XML instance for a user
/// supplied .NET type. See usage for details.
///
class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
// Check args
if(args.Length < 3)
{
PrintUsage();
System.Environment.Exit(1);
}
// Gather args
string sourceAssembly = args[0];
string typeName = args[1];
string outFile = args[2];
// Get schema
string schema = GetSchemaFromType(sourceAssembly, typeName);
Console.WriteLine(schema);
// Write instance
CreateXmlInstance(schema, outFile);
}
private static void PrintUsage()
{
System.Console.WriteLine("Usage: {0} sourceAssembly typeName instanceFilename", Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location));
}
private static string GetSchemaFromType(string file, string typeName)
{
XmlReflectionImporter importer = new XmlReflectionImporter();
XmlSchemas schemas = new XmlSchemas();
XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
// Enumerate the types, looking for the requested one
Assembly assem = Assembly.LoadFrom(file);
Type[] allTypes = assem.GetTypes();
for(int i = 0; i < allTypes.Length; i++)
{
Type t = allTypes[i];
if(t.IsPublic && !t.IsInterface)
{
if (t.FullName == typeName || t.Name == typeName)
{
XmlTypeMapping xtm = importer.ImportTypeMapping(t);
exporter.ExportTypeMapping(xtm);
break;
}
}
}
StringBuilder sb = new StringBuilder();
if(schemas.Count > 0)
{
StringWriter sw = new StringWriter(sb);
schemas[0].Write(sw);
sw.Close();
}
return sb.ToString();
}
private static void CreateXmlInstance(string schema, string outFile)
{
string errorString = "";
// Load the schema
Microsoft.BizTalk.TOM.CEditorSchemaTree edTree = new CEditorSchemaTree();
edTree.LoadFromString(schema, "", out errorString);
edTree.DetermineDisplayRootReferenceOfSchema();
InstanceGenerationOptions igo = new InstanceGenerationOptions();
igo.CycleExpansionDepth = 0;
igo.MaxNodeCount = 0;
igo.UseNativeExtension = false;
ITOMErrorInfo[] errors = null;
// Write the instance
edTree.CreateXMLInstance(outFile, igo, out errors);
}
}
}