Here is technique for generating an instance of a BizTalk message (a.k.a, XmlDocument) based on a deployed schema. It requires references to the following assemblies:
| Assembly |
Location |
| Microsoft.BizTalk.Pipeline |
%Program Files%/Microsoft BizTalk Server |
| Microsoft.BizTalk.ExplorerOM |
%Program Files%/Microsoft BizTalk Server/Developer Tools |
In a class library that has references to the above libraries, create a public class with the following two public methods:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.ExplorerOM;
namespace Spike.Sandbox
{
public class Utilities
{
/// <summary>
/// Build a BizTalk message for a single root node message
/// </summary>
/// <param name="schemaName"></param>
/// <returns></returns>
public static XmlDocument CreateBTSMessage(
string schemaName)
{
return CreateMessage(schemaName, schemaName);
}
/// <summary>
/// Build a BizTalk Message for a multi-root node message
/// </summary>
/// <param name="schemaName"></param>
/// <param name="rootNode"></param>
/// <returns></returns>
public static XmlDocument CreateBTSMessage(
string schemaName,
string rootNode)
{
return CreateMessage(schemaName,
string.Format(
"{0}+{1}", schemaName, rootNode));
}
These two methods call a private method that accesses the BizTalk management database to retrieve a deployed schema and construct the message:
private static XmlDocument CreateMessage(
string schemaName,
string rootNode)
{
XmlDocument doc =
new XmlDocument();
DocumentSpec docSpec;
BtsCatalogExplorer catalog =
new BtsCatalogExplorer();
catalog.ConnectionString =
"Data Source=myServer;Initial Catalog=BizTalkMgmtDb2;Integrated Security=True";
Schema schema = catalog.Schemas[schemaName];
if (schema !=
null)
{
docSpec =
new DocumentSpec(rootNode, schema.BtsAssembly.DisplayName);
if (docSpec !=
null)
{
StringBuilder sb =
new StringBuilder();
StringWriter sw =
new StringWriter(sb);
try
{
doc.Load(docSpec.CreateXmlInstance(sw));
}
finally
{
sw.Dispose();
}
}
}
return doc;
}
Cheers to Tomas Restrepo for his insight into the CreateXmlInstance: http://www.winterdom.com/weblog/2008/07/16/CreateXmlInstanceWithMultiRootSchemas.aspx
Posted
Fri, Aug 1 2008 7:15 AM
by
chilberto