1

我有以下代码片段

this.xslCompiledTransform = new XslCompiledTransform();

// initialize xsl transform
using (Stream xsltStream = currentAssembly.GetManifestResourceStream(this.TransformationFile))
using (XmlReader schemaReader = XmlReader.Create(xsltStream))
{
    XsltSettings settings = new XsltSettings() { EnableScript = true };
    this.xslCompiledTransform.Load(schemaReader, settings, null);
}

它有时会抛出 NullReferenceExcecption,有时它适用于同一个 xslt 文件。

你能告诉我上面的代码有什么问题吗?任何帮助将不胜感激。

下面是堆栈跟踪

    Object reference not set to an instance of an object.

Stack Trace:
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitFor(QilIterator ndFor)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisitEnsureStack(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitXPathNodeValue(QilUnary ndVal)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisitEnsureStack(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitXsltInvokeEarlyBound(QilInvokeEarlyBound ndInvoke)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitTextCtor(QilUnary ndText, Boolean disableOutputEscaping)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitLoop(QilLoop ndLoop)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitElementCtor(QilBinary ndElem)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitElementCtor(QilBinary ndElem)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitSequence(QilList ndSeq)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitElementCtor(QilBinary ndElem)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.VisitConditional(QilTernary ndCond)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd, Type itemStorageType, Boolean isCached)
   at System.Xml.Xsl.IlGen.XmlILVisitor.NestedVisit(QilNode nd)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Function(QilFunction ndFunc)
   at System.Xml.Xsl.IlGen.XmlILVisitor.Visit(QilExpression qil, GenerateHelper helper, MethodInfo methRoot)
   at System.Xml.Xsl.XmlILGenerator.Generate(QilExpression query, TypeBuilder typeBldr)
   at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
4

1 回答 1

1

直接从XslCompiledTransform.Load 方法注释部分复制:

在 Debug 模式下编译的 XSLT 和在 Release 模式下编译的 XSLT 是有区别的。在某些情况下,在 Debug 模式下编译的样式表在 Load 期间不会抛出错误,但稍后会在 Transform 期间失败。在 Release 模式下编译的相同样式表在 Load 期间将失败。这种行为的一个例子是当一个不是节点集类型的变量被分配给一个需要节点集的表达式时。

于 2012-05-29T05:57:09.800 回答