我有一些抛出XslTransformException的代码。这是所需的行为(XSL包含一个xsl:message元素,其中@terminate设置为yes)。我想在我的代码中捕获这个异常,但找不到包含此异常类的程序集,并且无法在MSDN中找到有关此异常的任何文档以获取合适的继承类(即避免在我的catch块中使用类Exception)。什么程序集是XslTransformException?
我得对System.Xml和Sytem.Xml.Linq组件引用,并具有以下using语句:
using System.Xml;
using System.Xml.Xsl;
唯一的例外是System.Xml.Xsl命名空间中;即:
System.Xml.Xsl.XslTransformException
任何想法我需要参考哪个程序集?
编辑: 按照要求,请看下面的示例代码重现此异常:当我看到在命名空间中没有提到XslTransformException
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
namespace StackOverflowDemo
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmsl = new XmlDocument();
xmsl.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" exclude-result-prefixes=\"msxsl\"><xsl:output method=\"xml\" indent=\"yes\"/><xsl:template match=\"@* | node()\"><xsl:message terminate=\"yes\">this should throw an exception</xsl:message><xsl:copy><xsl:apply-templates select=\"@* | node()\"/></xsl:copy></xsl:template></xsl:stylesheet>");
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(xmsl.CreateNavigator());
XmlDocument xml = new XmlDocument();
xml.LoadXml("<root />");
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
/*
try
{
*/
xsl.Transform(xml.CreateNavigator(), writer);
/*
}
catch(XslTransformException e) //<-- this class does not exist
{
Console.WriteLine(e.ToString());
}
*/
}
}
}
您是否有一个简短但完整的程序来重现异常,所以我们可以修补?我想知道异常类型是内部的,但是来自外部类型(例如XsltException。) –
不用担心@JonSkeet,请参阅上面的(修订问题)。 – JohnLBevan
JohnLBevan,我想知道为什么你需要知道在哪个程序集中定义了异常,以便捕捉它?这可以通过'try {...} catch()'完成。 –