创建自定义 biztalk 管道组件以将 HTML 转换为 XML?
我正在使用 BizTalk 服务器 2006(R2) 和 Visual Studio 2005, 在我的应用程序中,我有一个 html,必须在自定义管道组件中将其转换为 XML 我正在传递这样的 html,
<HTML><Customer><Data><Name>Udaya</Name><Age>18</Age><Nation>India</Nation></Data></Customer></HTML>
我必须像这样以 XML 形式输出
<ns0:Customer xmlns:ns0="http://CWW.com">
<Data>
<Name>Udaya</Name>
<Age>18</Age>
<Nation>India</Nation>
</Data>
</ns0:Customer>
谁能给我一些建议 有没有其他方法可以在不使用自定义管道的情况下执行相同的操作?
我正在尝试使用下面的管道组件,但它不起作用
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.ComponentModel;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Message.Interop;
//using System.Windows.Forms;
using System.CodeDom;
using HtmlAgilityPack;
namespace MessageBatchPipelineCompoent
{
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]
[System.Runtime.InteropServices.Guid("6118B8F0-8684-4ba2-87B4-8336D70BD4F7")]
public class DisassemblePipeline : IBaseComponent,
IDisassemblerComponent,
IComponentUI,
IPersistPropertyBag
{
//Used to hold disassembled messages
private System.Collections.Queue qOutputMsgs = new System.Collections.Queue();
private string systemPropertiesNamespace = @"http://schemas.microsoft.com/BizTalk/2003/system-properties";
/// <summary>
/// Batch size used to batch records
/// </summary>
private int _BatchSize;
public int BatchSize
{
get { return _BatchSize; }
set { _BatchSize = value; }
}
/// <summary>
/// Default constructor
/// </summary>
public DisassemblePipeline()
{
}
/// <summary>
/// Description of pipeline
/// </summary>
public string Description
{
get
{
return "Component to convert HTML to XML";
}
}
/// <summary>
/// Name of pipeline
/// </summary>
public string Name
{
get
{
return "HTMLToXMLComponent";
}
}
/// <summary>
/// Pipeline version
/// </summary>
public string Version
{
get
{
return "1.0.0.0";
}
}
/// <summary>
/// Returns collecton of errors
/// </summary>
public System.Collections.IEnumerator Validate(object projectSystem)
{
return null;
}
/// <summary>
/// Returns icon of pipeline
/// </summary>
public System.IntPtr Icon
{
get
{
return new System.IntPtr();
}
}
/// <summary>
/// Class GUID
/// </summary>
public void GetClassID(out Guid classID)
{
classID = new Guid("ACC3F15A-C389-4a5d-8F8E-2A951CDC4C19");
}
/// <summary>
/// InitNew
/// </summary>
public void InitNew()
{
}
/// <summary>
/// Load property from property bag
/// </summary>
public void Load(IPropertyBag propertyBag, int errorLog)
{
object val = null;
try
{
propertyBag.Read("BatchSize", out val, 0);
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
throw new ApplicationException("Error reading PropertyBag: " + ex.Message);
}
if (val != null)
_BatchSize = (int)val;
else
_BatchSize = 1;
}
/// <summary>
/// Write property to property bag
/// </summary>
public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
{
object val = (object)BatchSize;
propertyBag.Write("BatchSize", ref val);
}
/// <summary>
/// Disassembles (breaks) message into small messages as per batch size
/// </summary>
public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
{
string originalDataString;
try
{
//fetch original message
Stream originalMessageStream = pInMsg.BodyPart.GetOriginalDataStream();
byte[] bufferOriginalMessage = new byte[originalMessageStream.Length];
originalMessageStream.Read(bufferOriginalMessage, 0, Convert.ToInt32(originalMessageStream.Length));
originalDataString = System.Text.ASCIIEncoding.ASCII.GetString(bufferOriginalMessage);
}
catch (Exception ex)
{
throw new ApplicationException("Error in reading original message: " + ex.Message);
}
//XmlDocument originalMessageDoc = new XmlDocument();
//HtmlDocument originalMessageDoc = new HtmlDocument();
HtmlAgilityPack.HtmlDocument originalMessageDoc = new HtmlAgilityPack.HtmlDocument();
originalMessageDoc.Load(originalDataString);
StringBuilder messageString;
try
{
//load original message
// HtmlNode.
// originalMessageDoc.GetElementsByTagName("HTML");
//originalMessageDoc.DocumentElement.FirstChild.RemoveChild(originalMessageDoc.DocumentElement.FirstChild);
// originalMessageDoc.DocumentElement.FirstChild.Attributes.Append("ns0");
// String RootElement = originalMessageDoc.DocumentElement.Name;
messageString = new StringBuilder();
String RootNode = originalMessageDoc.DocumentNode.Name;
System.Diagnostics.EventLog.WriteEntry("Hello", "What doing");
//originalMessageDoc.DocumentNode.Attributes(RootNode,ns0);
//messageString.Insert();
messageString.Append(true);
// messageString.Append("<" + RootNode + " xmlns:ns0='" + "http://SomeContent" + "'>");
//messageString.Append("</" + "Sample" + ">");
CreateOutgoingMessage(pContext, messageString.ToString(), "http://SomeContent", "Sample", pInMsg);
}
catch (Exception ex)
{
throw new ApplicationException("Error in writing outgoing messages: " + ex.Message);
}
finally
{
messageString = null;
originalMessageDoc = null;
}
}
/// <summary>
/// Used to pass output messages`to next stage
/// </summary>
public IBaseMessage GetNext(IPipelineContext pContext)
{
if (qOutputMsgs.Count > 0)
return (IBaseMessage)qOutputMsgs.Dequeue();
else
return null;
}
/// <summary>
/// Queue outgoing messages
/// </summary>
private void CreateOutgoingMessage(IPipelineContext pContext, String messageString, string namespaceURI, string rootElement, IBaseMessage pInMsg)
{
IBaseMessage outMsg;
try
{
//create outgoing message
outMsg = pContext.GetMessageFactory().CreateMessage();
outMsg.Context = pInMsg.Context;
outMsg.AddPart("Body", pContext.GetMessageFactory().CreateMessagePart(), true);
outMsg.Context.Promote("MessageType", systemPropertiesNamespace,"Sample");
// outMsg.Context.Promote("MessageType", systemPropertiesNamespace, "http://SomeText" + "#" + rootElement.Replace("ns0:", ""));
byte[] bufferOoutgoingMessage = System.Text.ASCIIEncoding.ASCII.GetBytes(messageString);
outMsg.BodyPart.Data = new MemoryStream(bufferOoutgoingMessage);
qOutputMsgs.Enqueue(outMsg);
}
catch (Exception ex)
{
throw new ApplicationException("Error in queueing outgoing messages: " + ex.Message);
}
}
}
}
这是我创建的组件,但它不起作用。
现在我遇到了一个错误,比如路径中存在非法字符。
I am using BizTalk server 2006(R2) and Visual Studio 2005,
In my application i have a html which has to be converted into XML in a customized pipeline component
I am passing a html like this,
<HTML><Customer><Data><Name>Udaya</Name><Age>18</Age><Nation>India</Nation></Data></Customer></HTML>
I must get a out as XML like this
<ns0:Customer xmlns:ns0="http://CWW.com">
<Data>
<Name>Udaya</Name>
<Age>18</Age>
<Nation>India</Nation>
</Data>
</ns0:Customer>
Can anyone give me some suggestion is there any other way to do the same without using customized pipeline?
I am trying with the below pipeline component, but it doesn't work
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.ComponentModel;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Message.Interop;
//using System.Windows.Forms;
using System.CodeDom;
using HtmlAgilityPack;
namespace MessageBatchPipelineCompoent
{
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]
[System.Runtime.InteropServices.Guid("6118B8F0-8684-4ba2-87B4-8336D70BD4F7")]
public class DisassemblePipeline : IBaseComponent,
IDisassemblerComponent,
IComponentUI,
IPersistPropertyBag
{
//Used to hold disassembled messages
private System.Collections.Queue qOutputMsgs = new System.Collections.Queue();
private string systemPropertiesNamespace = @"http://schemas.microsoft.com/BizTalk/2003/system-properties";
/// <summary>
/// Batch size used to batch records
/// </summary>
private int _BatchSize;
public int BatchSize
{
get { return _BatchSize; }
set { _BatchSize = value; }
}
/// <summary>
/// Default constructor
/// </summary>
public DisassemblePipeline()
{
}
/// <summary>
/// Description of pipeline
/// </summary>
public string Description
{
get
{
return "Component to convert HTML to XML";
}
}
/// <summary>
/// Name of pipeline
/// </summary>
public string Name
{
get
{
return "HTMLToXMLComponent";
}
}
/// <summary>
/// Pipeline version
/// </summary>
public string Version
{
get
{
return "1.0.0.0";
}
}
/// <summary>
/// Returns collecton of errors
/// </summary>
public System.Collections.IEnumerator Validate(object projectSystem)
{
return null;
}
/// <summary>
/// Returns icon of pipeline
/// </summary>
public System.IntPtr Icon
{
get
{
return new System.IntPtr();
}
}
/// <summary>
/// Class GUID
/// </summary>
public void GetClassID(out Guid classID)
{
classID = new Guid("ACC3F15A-C389-4a5d-8F8E-2A951CDC4C19");
}
/// <summary>
/// InitNew
/// </summary>
public void InitNew()
{
}
/// <summary>
/// Load property from property bag
/// </summary>
public void Load(IPropertyBag propertyBag, int errorLog)
{
object val = null;
try
{
propertyBag.Read("BatchSize", out val, 0);
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
throw new ApplicationException("Error reading PropertyBag: " + ex.Message);
}
if (val != null)
_BatchSize = (int)val;
else
_BatchSize = 1;
}
/// <summary>
/// Write property to property bag
/// </summary>
public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
{
object val = (object)BatchSize;
propertyBag.Write("BatchSize", ref val);
}
/// <summary>
/// Disassembles (breaks) message into small messages as per batch size
/// </summary>
public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
{
string originalDataString;
try
{
//fetch original message
Stream originalMessageStream = pInMsg.BodyPart.GetOriginalDataStream();
byte[] bufferOriginalMessage = new byte[originalMessageStream.Length];
originalMessageStream.Read(bufferOriginalMessage, 0, Convert.ToInt32(originalMessageStream.Length));
originalDataString = System.Text.ASCIIEncoding.ASCII.GetString(bufferOriginalMessage);
}
catch (Exception ex)
{
throw new ApplicationException("Error in reading original message: " + ex.Message);
}
//XmlDocument originalMessageDoc = new XmlDocument();
//HtmlDocument originalMessageDoc = new HtmlDocument();
HtmlAgilityPack.HtmlDocument originalMessageDoc = new HtmlAgilityPack.HtmlDocument();
originalMessageDoc.Load(originalDataString);
StringBuilder messageString;
try
{
//load original message
// HtmlNode.
// originalMessageDoc.GetElementsByTagName("HTML");
//originalMessageDoc.DocumentElement.FirstChild.RemoveChild(originalMessageDoc.DocumentElement.FirstChild);
// originalMessageDoc.DocumentElement.FirstChild.Attributes.Append("ns0");
// String RootElement = originalMessageDoc.DocumentElement.Name;
messageString = new StringBuilder();
String RootNode = originalMessageDoc.DocumentNode.Name;
System.Diagnostics.EventLog.WriteEntry("Hello", "What doing");
//originalMessageDoc.DocumentNode.Attributes(RootNode,ns0);
//messageString.Insert();
messageString.Append(true);
// messageString.Append("<" + RootNode + " xmlns:ns0='" + "http://SomeContent" + "'>");
//messageString.Append("</" + "Sample" + ">");
CreateOutgoingMessage(pContext, messageString.ToString(), "http://SomeContent", "Sample", pInMsg);
}
catch (Exception ex)
{
throw new ApplicationException("Error in writing outgoing messages: " + ex.Message);
}
finally
{
messageString = null;
originalMessageDoc = null;
}
}
/// <summary>
/// Used to pass output messages`to next stage
/// </summary>
public IBaseMessage GetNext(IPipelineContext pContext)
{
if (qOutputMsgs.Count > 0)
return (IBaseMessage)qOutputMsgs.Dequeue();
else
return null;
}
/// <summary>
/// Queue outgoing messages
/// </summary>
private void CreateOutgoingMessage(IPipelineContext pContext, String messageString, string namespaceURI, string rootElement, IBaseMessage pInMsg)
{
IBaseMessage outMsg;
try
{
//create outgoing message
outMsg = pContext.GetMessageFactory().CreateMessage();
outMsg.Context = pInMsg.Context;
outMsg.AddPart("Body", pContext.GetMessageFactory().CreateMessagePart(), true);
outMsg.Context.Promote("MessageType", systemPropertiesNamespace,"Sample");
// outMsg.Context.Promote("MessageType", systemPropertiesNamespace, "http://SomeText" + "#" + rootElement.Replace("ns0:", ""));
byte[] bufferOoutgoingMessage = System.Text.ASCIIEncoding.ASCII.GetBytes(messageString);
outMsg.BodyPart.Data = new MemoryStream(bufferOoutgoingMessage);
qOutputMsgs.Enqueue(outMsg);
}
catch (Exception ex)
{
throw new ApplicationException("Error in queueing outgoing messages: " + ex.Message);
}
}
}
}
This is the component i have created but this is not working.
Now I am having an error like illegal characters in path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以前我使用的是originalMessageDoc.Load(originalDataString);
在反汇编函数中,所以我在路径错误中遇到了非法字符,现在我已经解决了这个特定问题,购买将其替换为
现在我可以在事件日志中看到我的 xml 已创建,但仍然有一个小错误
什么时候
String RootNode = originalMessageDoc.DocumentNode.Name;
以前我是这样给出的,现在我做了硬核它
现在它说 rootnode 没有声明
previously i was using originalMessageDoc.Load(originalDataString);
in the dissassemble function so i was getting the illegal characters in path error ,Now i have solved that particular issue buy replacing it with
Now i can see in the event log that i my xml is created but still i have a minor error
when
String RootNode = originalMessageDoc.DocumentNode.Name;
Previously i have given like this now i did hard cored it
now it says rootnode is not declared