这是我为类似问题实现的解决方案:正如 Hugh 建议的那样,我使用从 XmlDocument 继承的帮助程序。
Xml 模板类
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml;
namespace Acme
{
[Serializable]
public class ResourceXmlDocument : XmlDocument
{
public ResourceXmlDocument(Type assemblyType, string resourceName, QueryValues queryValues)
{
try
{
Assembly callingAssembly = Assembly.GetAssembly(assemblyType);
if (null == callingAssembly)
{
throw new ResourceException("GetExecutingAssembly returned null");
}
Stream resourceStream = callingAssembly.GetManifestResourceStream(resourceName);
Load(resourceStream);
if (null == queryValues)
{
throw new ResourceException("queryValues not initialized");
}
if (queryValues.Keys.Count < 1)
{
throw new ResourceException("queryValues.Keys must have at least one value");
}
foreach (string querycondition in queryValues.Keys)
{
XmlNode conditionNode = this.SelectSingleNode(querycondition);
if (null == conditionNode)
{
throw new ResourceException(string.Format(CultureInfo.InvariantCulture, "Condition: '{0}' did not return a XmlNode", querycondition));
}
XmlAttribute valueAttribute = conditionNode.Attributes["value"];
if (null == valueAttribute)
{
throw new ResourceException(string.Format(CultureInfo.InvariantCulture, "Condition: '{0}' with attribute 'value' did not return an XmlAttribute ", querycondition));
}
valueAttribute.Value = queryValues[querycondition];
}
}
catch (Exception ex)
{
throw new ResourceException(ex.Message);
}
}
}
}
当然,我的示例针对value
要设置的固定属性,因此您必须根据自己的需要进行调整。
QueryValues 帮助器类
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Acme
{
[Serializable]
public class QueryValues : Dictionary<string, string>
{
public QueryValues()
{
}
protected QueryValues(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
Xml 模板
将 Xml 文档MyTemplate.xml添加到您的项目并将编译操作更改为Embedded Resource
以便 ResorceXmlDocument 可以通过反射加载它。
<?xml version="1.0" encoding="utf-8" ?>
<root>
<SomeOtherNode>some (fixed) value</SomeOtherNode>
<MyNodeName tablename="MyTableName" fieldname="MyFieldName" value="0" />
<YetAnotherNode>
<SubNode>Foo</SubNode>
</YetAnotherNode>
</root>
编排变量和消息
你需要声明
- `Acme.QueryValues` 类型的变量 *queryValues*
- `Acme.ResourceXmlDocument` 类型的变量 *resourceXmlDoc*
- `MySchemaType` 类型的消息
将其放在消息分配形状中
在构造消息形状中创建 MySchemaType 类型的消息 MyRequest
queryValues = new Acme.QueryValues();
queryValues.Add("//MyNodeName[@tablename='MyTableName' and @fieldname='MyFieldName']", "MyValueToSet");
resourceXmlDoc = new Acme.ResourceXmlDocument(typeof(Acme.MySchemaType), "MyTemplate.xml", queryValues);
MyRequest = resourceXmlDoc;
我保存在一个 util lib 中,ResourceXmlDocument
并QueryValues
从我需要的任何 BizTalk 项目中引用它。各种 Xml 模板文档嵌入到各自的 BizTalk 程序集中。
由 OP 编辑:实际上,我进行这项工作的唯一方法是使用 OuterXml 的自定义序列化来实现和持久化消息ISerializable
。ResourceXmlDocument
基础中的 XmlDocument 本身根本无法序列化。如果有其他方法,请随时编辑。
[Serializable]
public class ResourceXmlDocument : XmlDocument, ISerializable
{
...
protected ResourceXmlDocument(SerializationInfo info, StreamingContext context)
{
if (info == null) throw new System.ArgumentNullException("info");
Load(info.GetString("content"));
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null) throw new System.ArgumentNullException("info");
info.AddValue("content", this.OuterXml);
}