Validating any XML Document within Orchestration

Recently, I needed to validate any XML documents within BizTalk.  I tried, but it was too limiting in that it required the SchemaStrongName property of the message, which, for a message of type XmlDocument, was not the actual schema's name.  Since we wanted to validate against any XmlDocument message, I had to come up with another solution.

What I did was, in a helper project, reference our BizTalk schemas assembly and pre-load all the schemas in the that assembly at class load time into an XmlSchemaSet (this is a.NET 2.0 solution, there should be a similar solution for 1.1):

private readonly static XmlSchemaSet _schemaSet;

static XMLHelper{  _schemaSet = new XmlSchemaSet;  Assembly schemaAssembly = Assembly.GetAssembly(typeof(MyBizTalkProject.Schemas.SomeSchema));  foreach (Type type in schemaAssembly.GetTypes)  {    if (typeof(SchemaBase).IsAssignableFrom(type) &&!type.IsNested)       _schemaSet.Add((Activator.CreateInstance(type) as SchemaBase).Schema);  } }

Then, a simple validation function can validate any XML document against any of the schemas in your schema project:

public bool ValidateDocument(XmlDocument businessDocument) {     ValidationHelper helper = new ValidationHelper;     bool isValid = helper.Validate(businessDocument, _schemaSet);     return isValid; }

public class ValidationHelper {     private bool _isValid = true;

    public bool Validate(XmlDocument document, XmlSchemaSet schema)     {         ValidationEventHandler eventHandler = new ValidationEventHandler(HandleValidationError);        document.Schemas = schema;        document.Validate(eventHandler);        return _isValid;     }

    private void HandleValidationError(object sender, ValidationEventArgs ve)     {          _isValid = false;     }   }

This article is part of the GWB Archives. Original Author: Don Tian

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.