Reputation: 269
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);
}
Upvotes: 0
Views: 1862
Reputation: 58522
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.
Upvotes: 1