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 PARTS_IN_SCHEMA_STRONG_NAME = 2;
const int PART_CLASS_NAME = 0;
const int PART_QUALIFIED_ASSEMBLY_NAME = 1;
// 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 ) );