C# xslt BizTalk 错误对象引用未设置到实例

发布于 2024-10-30 08:18:06 字数 2533 浏览 5 评论 0原文

我正在使用以下代码转换 xml。 它对于一个 xslt 工作正常,但会抛出异常:未将对象引用设置为对象的实例。 我已经验证了两个 xslt,它们单独工作正常(意味着在转换后生成预期的 xml)。 有人可以指导我找出这里出了什么问题吗?我确实尝试进行一些调试,但无法获取错误详细信息或堆栈跟踪,因为它是部署在服务器上的 BizTalk 应用程序....提前致谢 这是代码。

 public static XmlDocument ApplyTransform(
       XmlDocument toTransform,
       XmlDocument StyleSheet)
   {
       XslCompiledTransform xslt = new XslCompiledTransform();
       XmlDocument transformedDoc = new XmlDocument();
       Stream stream = new MemoryStream();
       StreamWriter sw = new StreamWriter(stream);

       log4net.Ext.Serializable.SLog logger;
       logger = log4net.Ext.Serializable.SLogManager.GetLogger(@"BizTalk", typeof(RuntimeFileReader));
       logger.RegistryConfigurator();

       string logMsg = string.Format("StyleSheet used: {0}", StyleSheet);
       logger.Debug(logMsg);

       try
       {
           xslt.Load(StyleSheet);
           xslt.Transform(toTransform, null, sw);
           stream.Seek(0, SeekOrigin.Begin);
           transformedDoc.Load(stream);
       }
       catch
       {
           return null;
       }
       finally
       {
           if (sw != null)
           {
           sw.Flush();
           sw.Close();
           }
       }
       string gMsg = string.Format("xml after Transformation : {0}", transformedDoc.OuterXml);
       logger.Debug(gMsg);

       return transformedDoc;
   }

这是抛出异常的 XSLT。

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="Item"/>
  <xsl:template match="node()|@*" xml:space="default">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="errorCodes"/>
</xsl:stylesheet>

商务通代码:

PIToIMTransform = new System.Xml.XmlDocument();     
PIToIMTransform.Load(
                      gh.BizTalk.Components.RuntimeFileReader
                      .GetResourceFilePath("­PInode.xslt"));
if (logger.IsDebugEnabled) {
      xmlDoc = PIToIMTransform; 
      logger.DebugFormat(logProps, "XSLT being used for transform: {0}", xmlDoc.OuterXml);
}
xmlDoc = gh.BizTalk.Components
           .XmlUtility.ApplyTransform(PItransformedDoc, PIToIMTransform); 
if (logger.IsDebugEnabled){
       logger.DebugFormat(logProps, "PI Message AFTER removed errorCodes transform: {0}"
              , xmlDoc.OuterXml);
} 

I'm transforming xml using the following code.
It works fine for one xslt but it throws exception: Object reference not set to an instance of an object.
I have validated both xslt's they are working fine individually (meaning generating expected xml after transformation).
Can someone guide me to figure out what is wrong here. I did try to do some debug but can't get the error details or stack trace since it is a BizTalk app which is deployed on the server .... Thanks in advance
Here is the code.

 public static XmlDocument ApplyTransform(
       XmlDocument toTransform,
       XmlDocument StyleSheet)
   {
       XslCompiledTransform xslt = new XslCompiledTransform();
       XmlDocument transformedDoc = new XmlDocument();
       Stream stream = new MemoryStream();
       StreamWriter sw = new StreamWriter(stream);

       log4net.Ext.Serializable.SLog logger;
       logger = log4net.Ext.Serializable.SLogManager.GetLogger(@"BizTalk", typeof(RuntimeFileReader));
       logger.RegistryConfigurator();

       string logMsg = string.Format("StyleSheet used: {0}", StyleSheet);
       logger.Debug(logMsg);

       try
       {
           xslt.Load(StyleSheet);
           xslt.Transform(toTransform, null, sw);
           stream.Seek(0, SeekOrigin.Begin);
           transformedDoc.Load(stream);
       }
       catch
       {
           return null;
       }
       finally
       {
           if (sw != null)
           {
           sw.Flush();
           sw.Close();
           }
       }
       string gMsg = string.Format("xml after Transformation : {0}", transformedDoc.OuterXml);
       logger.Debug(gMsg);

       return transformedDoc;
   }

And here is the XSLT which throwing exception.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="Item"/>
  <xsl:template match="node()|@*" xml:space="default">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="errorCodes"/>
</xsl:stylesheet>

Biztalk Code:

PIToIMTransform = new System.Xml.XmlDocument();     
PIToIMTransform.Load(
                      gh.BizTalk.Components.RuntimeFileReader
                      .GetResourceFilePath("­PInode.xslt"));
if (logger.IsDebugEnabled) {
      xmlDoc = PIToIMTransform; 
      logger.DebugFormat(logProps, "XSLT being used for transform: {0}", xmlDoc.OuterXml);
}
xmlDoc = gh.BizTalk.Components
           .XmlUtility.ApplyTransform(PItransformedDoc, PIToIMTransform); 
if (logger.IsDebugEnabled){
       logger.DebugFormat(logProps, "PI Message AFTER removed errorCodes transform: {0}"
              , xmlDoc.OuterXml);
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-11-06 08:18:06

当您返回 BizTalk 时,您引用了一个 null 对象并崩溃(xmlDoc.OuterXml),xmlDoc 为 null,并且基本上执行了 null.OuterXml

mlDoc = gh.BizTalk.Components
       .XmlUtility.ApplyTransform(PItransformedDoc, PIToIMTransform); 
if (logger.IsDebugEnabled){
   logger.DebugFormat(logProps, "PI Message AFTER removed errorCodes transform: {0}"
          , xmlDoc.OuterXml);
} 

建议是保护xmlDoc 调用,并在异常处理块中打印出正在抛出的异常。您的关键部分出了问题,可能是解析错误,也可能是 xsl 加载错误,请尝试以下操作:

   catch (Exception e)
   {
       logger.Error(e);
       return ;
   }

然后让我们知道抛出的异常。

When you get back to BizTalk you reference a null object and crash(the xmlDoc.OuterXml), xmlDoc is null and basically do a null.OuterXml:

mlDoc = gh.BizTalk.Components
       .XmlUtility.ApplyTransform(PItransformedDoc, PIToIMTransform); 
if (logger.IsDebugEnabled){
   logger.DebugFormat(logProps, "PI Message AFTER removed errorCodes transform: {0}"
          , xmlDoc.OuterXml);
} 

Suggestion is to protect the xmlDoc call, and also print out in your exception handling block the exception that is being thrown. Something is going wrong in your critical section it could be a parse error, it could be an xsl load error try this:

   catch (Exception e)
   {
       logger.Error(e);
       return ;
   }

And then let us know the exception that is being thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文