I have some code which is throwing an XslTransformException. This is the desired behavior (the XSL contains an xsl:message element with @terminate set to yes).
I'm trying to catch this exception in my code, but can't find the assembly containing this exception's class, and can't find any documentation on this exception in MSDN to get an idea of a suitable inherited class (i.e. to avoid using the class Exception in my catch block).
I've got the System.Xml and Sytem.Xml.Linq assemblies referenced and have the following using statements:
using System.Xml;
using System.Xml.Xsl;
The exception is in the System.Xml.Xsl namespace; i.e.:
System.Xml.Xsl.XslTransformException
Any idea which assembly I need to reference?
EDIT: As requested, please find below sample code to reproduce this exception:
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());
}
*/
}
}
}
Adding the following catch
block reveals all:
catch (Exception e)
{
var t = e.GetType();
while (t != null)
{
Console.WriteLine(t.AssemblyQualifiedName);
t = t.BaseType;
}
}
Output:
System.Xml.Xsl.XslTransformException, System.Data.SqlXml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Xml.Xsl.XsltException, System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.SystemException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
I would ignore the XslTransformException
though - you should catch XsltException
instead. After all, that's what XslCompiledTransform.Transform
is documented to throw.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With