我有一些拋出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()'完成。 –