Saturday, September 18, 2004

Cool XSD Validation Function for Orchestration 

As I promised in an earlier post, here's a cool XSD validation function that can be called from within orchestration. Just add to a library - I tend to put it into a BizTalk Utilities library full of useful little functions that complement orchestration (most of which should be provided by the orchestration engine, but that's a rant for another day!).

public static void ValidateDocument( XmlDocument businessDocument, string schemaStrongName )
  // Constants
  const int PART_CLASS_NAME = 0;

  // Parse schema strong name
  string[] assemblyNameParts = schemaStrongName.Split( new char[] { ',' }, PARTS_IN_SCHEMA_STRONG_NAME );
  string className = assemblyNameParts[PART_CLASS_NAME].Trim();
  string fullyQualifiedAssemblyName = assemblyNameParts[PART_QUALIFIED_ASSEMBLY_NAME].Trim();

  // Load assembly
  Assembly schemaAssembly = Assembly.Load( fullyQualifiedAssemblyName );

  // Create instance of the BTS schema in order to get to the actual schemas
  Type schemaType = schemaAssembly.GetType( className );
  Microsoft.XLANGs.BaseTypes.SchemaBase btsSchemaCollection = ( Microsoft.XLANGs.BaseTypes.SchemaBase )Activator.CreateInstance( schemaType );

  // Set up XML validating reader and validate document
  XmlParserContext parserContext = new XmlParserContext( null, null, "", XmlSpace.None );
  XmlValidatingReader reader = new XmlValidatingReader( businessDocument.OuterXml, XmlNodeType.Document, parserContext );
  reader.ValidationType = ValidationType.Schema;
  reader.Schemas.Add( btsSchemaCollection.SchemaCollection );
  while( reader.Read() ) {}

In order to call in orchestration, just create an expression shape and use the following:

ValidateDocument( myBtsMessage, myBtsMessage( BTS.SchemaStrongName ) );

About the Author

You may be wondering who I am (or may not!), but I've been in the industry 16+ years working on a variety of systems from IBM mainframes as a CICS systems programmer, to developing on Unix and Windows based systems. At the moment, I'm currently working for a Microsoft Gold Partner in the UK called Solidsoft who specialise in systems integration using BizTalk Server. My position is generally dictated by what I'm doing, but normally as a solutions architect/consultant helping clients with their integration projects involving, yes you guessed it: BizTalk Server.

Disclaimer: all the views expressed here are my own and do not necessarily represent the views of Solidsoft Ltd or indeed any other company.