首页 > 解决方案 > 在 Azure Logic Apps 中转换 XML,将 C# 程序集 (.DLL) 文件上传到 Azure 集成帐户并在 XSLT(地图)中使用它

问题描述

在 Log Apps 中,我有一个 Source.xml 文件,需要使用 XSLT 将其转换为另一个 destination.xml。在 XSLT 中,我需要使用 C# 代码实现一些自定义逻辑,这些代码将上传到 Azure 集成帐户“程序集部分”

我创建了一个带有 StrongName 的 C# 程序集文件,但需要了解如何从 XSLT 调用 C# 程序集的方法。

如果有人可以通过一个非常基本的示例分享一个工作示例代码,将不胜感激。

标签: c#azurexsltassembliesazure-logic-apps

解决方案


我找到了一个示例供您参考:

c#代码如下所示:

using System;
namespace ExtAssembly
{
 public static class CalcFunctions
 {
 public static Int64 Add(Int64 a, Int64 b)
 {
 return a + b;
 }
 public static Int64 Subtract(Int64 a, Int64 b)
 {
 return a - b;
 }
 public static Int64 Multiply(Int64 a, Int64 b)
 {
 return a * b;
 }
 public static Double Divide(Int64 a, Int64 b)
 {
 return a / b;
 }
 }
}

我们可以在 xslt 中使用 c# 代码,如下所示:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform Jump " xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl userCSharp" version="1.0" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp Jump ">
 <xsl:output omit-xml-declaration="yes" media-type="application/text" method="text" version="1.0" />
 <xsl:param name="MethodName" />
 <xsl:param name="Parameters" />
 <xsl:template match="/">
 <xsl:value-of select ="userCSharp:Invoke($MethodName, substring-before(substring-after($Parameters, '('), ')'))" />
 </xsl:template>
 <msxsl:script language="C#" implements-prefix="userCSharp"> 
 <msxsl:assembly name="ExtAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" />
 <msxsl:using namespace="System.Reflection" />
 <msxsl:using namespace="System.Text.RegularExpressions" />
 <msxsl:using namespace="ExtAssembly" />
 <![CDATA[
 public object Invoke(string methodName, string methodParameters)
 {
 MatchCollection matches = new Regex("((?<=\")[^\"]*(?=\"(,|$)+)|(?<=,|^)[^,\"]*(?=,|$))").Matches(methodParameters); 
 ParameterInfo[] pars = typeof(CalcFunctions).GetMethod(methodName).GetParameters();
 object[] methodPars = new object[pars.Length]; 
 for (int i = 0; i < pars.Length; i++)
 {
 methodPars[i] = Convert.ChangeType(matches[i].Value, pars[i].ParameterType);
 }
 return typeof(CalcFunctions).GetMethod(methodName).Invoke(null, methodPars);
 }
 ]]>
</msxsl:script>
</xsl:stylesheet>

之后,我们可以在我们的逻辑应用程序中调用它(在“Transform xml”操作中提供“MethodName”和“Parameters”)。

有关详细信息,您可以参考此示例

希望对您的问题有所帮助。


推荐阅读