Geeks With Blogs
Andy Morrison's BizTalk Blog Help, I've fallen and I can't BizTalk!

This post disappeared from a previous blog of mine so I'm reposting it.

---

 

I've been doing some research on custom adapter development and one of the issues that came up was figuring out how to do the schema generation.  BizTalk Server 2004 includes a wizard that will generate schemas from Xml instance documents.  I decided to figure out how it worked - and if it could be reused.

 

It is possible to reuse this functionality.  Here's how you do it:

 

  1. Create a console project.
  1. Reference the Microsoft.BizTalk.WFXToXSDGenerator.dll and XSDInfer.dll assemblies in the [installDir]\Microsoft BizTalk Server 2004\SDK\Utilities\Schema Generator directory. 
  1. Reference the Microsoft.BizTalk.SchemaEditor.Extensibility.dll assembly in the [installDir]\Program Files\Microsoft BizTalk Server 2004\Developer Tools directory.
  1. Pull in the Microsoft.BizTalk.WfxToXSDGenerator namespace.
  1. Add the following code to your Main method:

 

WfxToXSDGenerator gen = new WfxToXSDGenerator();

 

gen.GenerateSchema(args[0], args[1]);

 

object[] warnings = gen.Warnings();

object[] errors = gen.Errors();

object[] schemas = gen.ReferencedSchemas();

 

if(warnings.Length != 0)

{

Console.WriteLine("Warnings were found:");

 

          for(int i = 0; i < warnings.Length; i++)

          {

        Console.WriteLine(warnings[i].ToString());

          }

 

          Console.ReadLine();

          return;

}

 

if(errors.Length != 0)

{

Console.WriteLine("Errors were found:");

 

          for(int i = 0; i < warnings.Length; i++)

          {

        Console.WriteLine(errors[i].ToString());

          }

 

          Console.ReadLine();

          return;

}

 

if(schemas.Length !=0)

{

Console.WriteLine("The following schemas were generated:");

 

          for(int i = 0; i < schemas.Length; i++)

          {

        Console.WriteLine(schemas[i].ToString());

          }

 

          Console.ReadLine();

          return;

}

 

Notes:

  • The first argument to the GenerateSchema method is the location of the Xml instance document.  The second argument is the output directory for the generated schemas.
  • A Warning and Errors array is present; oddly you access these through methods, not properties.  I didn't run into any errors or warnings so I didn't investigate their Types; my assumption is that they are string but it would be easy for you to figure this out if you run into issues.
  • GenerateSchema() doesn't overwrite schemas you already created with it; it adds a n (a number starting with 1) to the end of the filename (i.e. sample1.xml)
  • There does appear to be another MS Xsd inference assembly out there (http://www.fawcette.com/xmlmag/2002_11/online/xml_rjennings_11_11_02/); perhaps the assembly is later version of this assembly.Should be fairly robust given this is what the Wizard uses (inferred from the directories.)
  • I did not investigate the DTD version of the generator.

 

Why would you use reuse this library?  A couple of thoughts come to mind: when automating batch schema generation and when writing custom application adapters.  Obviously, there are other resources available on the web (and within VS.NET) that can generate schemas from Xml instance documents but I assume that this library has been put through a rigorous QA process (although it is a sample according to this post http://groups-beta.google.com/group/microsoft.public.biztalk.sdk/browse_thread/thread/913c7fa57fc127a/2ac3418b674f5da8?q=ISchemaGenerator#2ac3418b674f5da8 [I wonder why it is in the utilities directory if it is a sample?]) given its usage in BizTalk Server 2004.

Posted on Wednesday, December 6, 2006 7:25 AM BizTalk | Back to top

Copyright © Andy Morrison | Powered by: GeeksWithBlogs.net