Validating SchemasBy default, BizTalk Server does not validate XML against a Schema. This is because the Validating Schemas function is assumed to be performed in the Receive Pipeline​. However, sometimes new messages are created within an Orchestration and those additional data streams need to be validated.

Let’s look at a solution which implies changing the BizTalk global configuration. By doing this to your installation, you may unknowingly be affecting the behavior of other applications in the server. Another approach is to generate a helper class that will validate the schema. The class may look similar to this:

public class SchemaValidator

{

private static object _lock = new object();

private static string _message;

private static HL.BizTalk.Edi.Schemas.Schema835.X12_00501_835 _schema = new Schemas.Schema835.X12_00501_835();

public static void Validate(XmlDocument businessDocument)

{

lock (_lock)

{

try

{

businessDocument.Schemas = new System.Xml.Schema.XmlSchemaSet();

businessDocument.Schemas.Add(((SchemaBase)_schema).Schema);

businessDocument.Validate(new System.Xml.Schema.ValidationEventHandler(HandleValidationError));

}

catch

{

throw;

}

if (!string.IsNullOrEmpty(_message))

{

string tmpMessage = _message;

_message = null;

throw new Exception(tmpMessage);

}

}

}

 

 

private static void HandleValidationError(object sender, ValidationEventArgs ve)

{

_message = ve.Exception.Message;

}

}

To consider while Validating Schemas

This class validates only a particular schema, but the code that loads the schema can be generic. You can use reflection to load the schema to be validated in each instance. This custom class can be modified to handle multiple schemas.

In order to make this work, follow these general guidelines:

  • Store the schemas in a Dictionary<string,  SchemaBase> where the key is the schema name.
  • Change the Validate method header to receive two parameters, the xml document and the schema name.
  • In the first line of Validate method. Search for the schema in the dictionary: i.e. schema = dictionary[schemaName];

Two other important things to keep in mind during your build:

  • It’s important to lock the code that executes the validation since this class is static and if you are using this validation in a loop or in a batch scenario, you may receive validation errors when actually the XML file you are validating is OK.
  • You can throw an exception or add a bool return value in the Validate method if you want to use it that way.

To call this data from an orchestration:

1) Add an expression shape and in the expression add a line similar to this (depending on your helpers namespace)

2) HL.BizTalk.Edi.Helpers.SchemaValidator.Validate(myMessage);

It’s important to know that this will only do schema validation (cardinalities, types). In scenarios where you are using schemas like swift or rosettanet, this approach may not be valid since the validations in the pipeline are more complex.

To learn more about Validating Schemas click here.

Share This