Martijn's blog - E-Commerce, EAI, BizTalk and .NET

2004/08/14

Retrieving the matching schema for an inbound message

Ever wanted to know which schema matches your inbound message from within a pipeline component? Here's how:

Reference Microsoft.BizTalk.Streaming,

Use the following code within your pipeline component:

Stream orgStream =
   inmsg.BodyPart.GetOriginalDataStream();

MarkableForwardOnlyEventingReadStream helperStream =
   new MarkableForwardOnlyEventingReadStream(orgStream);

IDocumentSpec docSpec = null;

try {
  docSpec =
     pc.GetDocumentSpecByType(
         Utils.GetDocType(helperStream));
}
catch(Exception e)
{
  Trace.WriteLine(e.ToString());
}

if(docSpec != null)
{
  XmlSchemaCollection schemaCollection =
     docSpec.GetSchemaCollection();
  
    // use the schema collection
}
else
{
  Trace.WriteLine(
     "No schemas found matching our inbound message");
}

Now you have the schema collection which matches your document (this code is minimal, real exception handling should be added ofcourse.

Notes:

Utils.GetDocType does nothing more then read the stream using a XmlTextReader and combine any found namespace with the root element name. If you have DOCTYPE declarations, you could implement this yourself to avoid having to put your DTDs in %windir%\system32.

The MarkableForwardOnlyEventingReadStream class helps in easily resetting the position back to where the stream was once the Utils.GetDocType is done.

Hope you find this useful!

0 Comments:

Post a Comment

<< Home