Geeks With Blogs
Don Tian .NET and BizTalk blog

Recently, I needed to validate any XML documents within BizTalk.  I tried this solution, 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;
    }  
}

Posted on Friday, July 28, 2006 6:49 AM BizTalk | Back to top


Comments on this post: Validating any XML Document within Orchestration

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Don Tian | Powered by: GeeksWithBlogs.net