Load XmlSchema’s from BizTalk assembly

December 7th, 2005 by Sander Schutten

That BizTalk compiles your XmlSchema’s into an assembly is great, but wouldn’t it be greater if you could reuse those schema’s in your code? No more hassle of different versions of your schema and your schema’s always at your fingertips because they are in the GAC. Well…it took me a while to figure that it’s actually really simple to do.

To load a schema from an external assembly just instantiate the class which describes your schema. This will be an instance with base type XmlSchemaWithNone. The instance has a property Schema which holds the XmlSchema. If you want to modify the schema I recommend using the CreateResolvedSchema() method on the instance you’ve just created because if you edit the Schema property directly, the schema will be altered and remain in memory until you restart your BizTalk group.

It’s allso possible to load the schema using reflection. This can be useful if you want to use a schema inside a custom pipeline component. Do this as followed:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
// Load the schema assembly
Assembly assembly = Assembly.Load(schemaWithNone.AssemblyName);
// Get the type of the xml schema we need
Type type = assembly.GetType(schemaWithNone.DocSpecName);
// Get schema property
PropertyInfo schemaInfo = type.GetProperty("Schema");
// Create instance of the schema
Object instance = assembly.CreateInstance(type.FullName);
// Get the XmlSchema
XmlSchema xmlSchema = (XmlSchema)schemaInfo.GetValue(instance, null);

Posted in BizTalk Server |

One Response

  1. Jim Says:

    this is awesome! but - see http://msdn2.microsoft.com/en-us/library/ms975678.aspx

    specifically this manual reference that needs to be added to project file

    \assembly\GAC\Microsoft.BizTalk.Component.Utilities\3.0.1.0__31bf3856ad364e35\Microsoft.BizTalk.Component.Utilities.dll”
    />

Leave a Comment


Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.