2011-04-05 48 views
0

我正在使用以下代碼轉換xml。 它適用於一個xslt,但它會拋出異常:對象引用未設置爲對象的實例。 我已驗證兩個xslt他們工作正常單獨(意味着生成轉換後預期的XML)。 有人可以指導我弄清楚這裏有什麼問題。我曾嘗試做一些調試,但無法獲取錯誤詳細信息或堆棧跟蹤,因爲它是部署在服務器上的BizTalk應用程序....預先感謝 以下是代碼。C#xslt未將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> 

BizTalk代碼:

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); 
} 
+0

你能訪問Biztalk服務器上的事件日誌嗎? – Nix 2011-04-05 14:35:12

+0

並且有代碼塊在biztalk裏面工作過嗎? – Nix 2011-04-05 14:36:46

+0

@Nix。我已經檢查過事件日誌沒有錯誤或警告。 – JohnXsl 2011-04-05 14:47:11

回答

1

當你回到到BizTalk你引用一個空對象和崩潰(在xmlDoc.OuterXml),xmlDoc中爲空,基本上做一個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調用,並打印出您的異常手ling阻止正在拋出的異常。事情錯在你的關鍵部分它可能是一個語法錯誤,它可能是一個xsl加載錯誤試試這個:

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

然後讓我們知道,被拋出的異常。

+0

&@Daniel謝謝你的幫助。我已經找到了問題。我在第二個XSLT中使用了。這是造成這個問題。因爲在你的幫助下,我能夠解決問題,所以我會將你的回答標記爲答案。再次感謝您的幫助。 – JohnXsl 2011-04-05 20:14:52

相關問題