构造消息 - 循环输入 [英] Contruct Message - Loop through input

查看:63
本文介绍了构造消息 - 循环输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在BizTalk中,我必须向Web服务发送消息并从中获取响应,因此我正在执行以下操作:

In BizTalk, I have to post a message to web service and get the response from it, so I am doing the following:

我从用户收到输入消息(我需要分配给Web服务的请求消息)。来自用户的输入消息如下所示

I receive the input messge from user (which I need to assign to web service's request message). The input message from user looks like this


<pre lang="x-xml"><PurchaseOrder>
<PurchaseOrderID>P123</PurchaseOrderID>
<ItemDetails>
<Item>
   <ItemCode>I001</ItemCode>
   <ItemQuantity>10</ItemCode>
</Item>
<Item>
   <ItemCode>I002</ItemCode>
   <ItemQuantity>30</ItemCode>
</Item>
<Item>
   <ItemCode>I003</ItemCode>
   <ItemQuantity>90</ItemCode>
</Item>
</ItemDetails>
<PurchaseOrderDate>18-Oct-2011</PurchaseOrderDate>
</PurchaseOrder>

推荐答案

建议创建输出模式并使用map进行转换。



但是,如果您的要求是在Orchestration(消息分配形状)内创建输出,那么您可以使用XSLT为您执行相同操作。我建议使用XSLT,因为输入xml中'Product'节点的出现是未定义的。使用XSLT循环遍历
节点比使用.NET代码循环更容易。

It is recommended to create a Output schema and do the transformation using map.

But, if your requirement is to create the Output inside Orchestration (Message Assignment Shape), then you can use XSLT to do the same for you. I'm suggesting XSLT because the occurance of 'Product' node in input xml is undefined. It is easier to loop through nodes using XSLT, than looping using .NET code.

在这种情况下,从Message Assignment Shape调用.NET Helper dll并传递输入xml字符串作为参数。下面提供了Helper DLL代码,它将执行XSLT并生成所需的输出字符串。

In this case you call a .NET Helper dll from Message Assignment Shape and pass the input xml string as parameter. Helper dll code is provided below, which will execute XSLT and generate the required output string.

XSLT Helper Code:

XSLT Helper Code:

 


using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace BizTalkHelperComponents
{
    public class XsltHelper
    {
        public string ExecuteXSLT(string xmlString)
        {
            MemoryStream memoryStream = null;
            Stream xslStream = null;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xmlString);

                // Create XPath Document from Input XML
                memoryStream = new MemoryStream();
                xmlDocument.Save(memoryStream);
                memoryStream.Position = 0;
                XPathDocument xPathDoc = new XPathDocument(memoryStream);

                // Load XSLT from Resources
                xslStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BizTalkHelperComponents.Map_PO_Input_To_Output.xsl");
                XmlReader xslReader = new XmlTextReader(xslStream);
                XslCompiledTransform mefXslTransform = new XslCompiledTransform();
                mefXslTransform.Load(xslReader);
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);

                // Apply XSL Transform on Input XML
                mefXslTransform.Transform(xPathDoc.CreateNavigator(), null, sw);
                sw.Flush();

                // Return Output XML string
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Dispose
                if (null != memoryStream)
                    memoryStream.Dispose();

                if (null != xslStream)
                    xslStream.Dispose();
            }
        }
    }
}


这篇关于构造消息 - 循环输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆