我有一些代码会抛出XslTransformException异常,这是期望的行为(XSL包含一个xsl:message元素,@terminate设置为是)。
我试图在我的代码中捕获此异常,但找不到包含此异常类的程序集,并且无法在MSDN上找到有关此异常的任何文档,以便了解适当的继承类(即避免在catch块中使用Exception类)。
我已经引用了System.Xml和Sytem.Xml.Linq程序集,并具有以下using语句:
我试图在我的代码中捕获此异常,但找不到包含此异常类的程序集,并且无法在MSDN上找到有关此异常的任何文档,以便了解适当的继承类(即避免在catch块中使用Exception类)。
我已经引用了System.Xml和Sytem.Xml.Linq程序集,并具有以下using语句:
using System.Xml;
using System.Xml.Xsl;
例外情况在 System.Xml.Xsl 命名空间中,即:
System.Xml.Xsl.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());
}
*/
}
}
}
try{...} catch()
来实现。 - Dimitre Novatchevusing
语句,以便在代码中只使用异常名称。 - Dimitre Novatchev