C# xslt BizTalk 错误对象引用未设置到实例
我正在使用以下代码转换 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您返回 BizTalk 时,您引用了一个 null 对象并崩溃(
xmlDoc.OuterXml
),xmlDoc 为 null,并且基本上执行了null.OuterXml
:建议是保护xmlDoc 调用,并在异常处理块中打印出正在抛出的异常。您的关键部分出了问题,可能是解析错误,也可能是 xsl 加载错误,请尝试以下操作:
然后让我们知道抛出的异常。
When you get back to BizTalk you reference a null object and crash(the
xmlDoc.OuterXml
), xmlDoc is null and basically do anull.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:
And then let us know the exception that is being thrown.