What assembly is XslTransformException in?
Asked Answered
O

3

5

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());
            }            
            */
        }
    }
}
Olcott answered 6/11, 2012 at 17:27 Comment(6)
Do you have a short but complete program which reproduces the exception, so we can tinker? I wonder whether the exception type is internal, but derives from an external type (e.g. XsltException.)Ballistics
No worries @JonSkeet, please see (revised question) above.Olcott
JohnLBevan, I wonder why would you need to know in which assembly is the exception defined, in order to catch it? This can be done just by try{...} catch() .Mckay
Hey @DimitreNovatchev. The reason for knowing he assembly is so you can reference the exception's class in the catch statement, thus only catching exceptions of that type (i.e. try{...}catch(Exception e){...} would catch all exceptions - best practice dictates you should be as specific as possible with you catch block; i.e. try{...}catch(SpecificException e){...}. The reason for this is exceptions other than the one you're handling may occur. You could catch the exception and implement a case statement, but that's not the correct way, so would cause confusion / should be a last resort.Olcott
@JohnLBevan, I have been working with exceptions for years and not a single time did I need to know what the assembly is. One does need to know the namespace for the exception class -- and have a using statement so that just the exception name could be used in the code.Mckay
@DimitreNovatchev Agreed that you also require a using statement, but if you haven't got a reference to the DLL (assembly) you can't see the class (in most cases you also can't see the namespace, but in this instance the namespace System.Xml.Xsl was used within the assembly System.Data.SqlXml. You can see this for yourself if you create 3 projects, 1 with an exception, 1 which references the exception project and throws an exception of this type, and another to catch the exception which references the throwing project but not the exception project. Projects are just source for assemblies.Olcott
B
5

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.

Ballistics answered 6/11, 2012 at 17:55 Comment(1)
Great, thanks @JonSkeet; that's a useful trick for next time I go assembly huntin too.Olcott
L
3

It's in System.Data.SqlXml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

The exception is internal though, so you won't be able to catch it directly. It extends XsltException, so as Jon Skeet mentioned, just catch XsltException.

Lykins answered 6/11, 2012 at 17:34 Comment(3)
No luck - I tried changing my project to both .net 4 and .net 4 client profile. C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.SqlXml.dll & C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.SqlXml.dllOlcott
It's there. It's just declared as internal, so you won't be able to catch it directly. I'm seeing it with ILSpy.Lykins
Ah, for some reason I'd assumed you wouldn't be able to throw internal exceptions externally, but would have to wrap them in exceptions with the same availability as the calling method's. Thanks @PabloRomeo.Olcott
C
0

While I see no reference to XslTransformException in the namespace, the rest of System.Xsl lives System.Xml.dll - which you can see on MSDN (I chose the first type in the namespace as an example) on the Assembly: line, just above the Syntax block.

Crumble answered 6/11, 2012 at 17:32 Comment(1)
Thanks @DanDaviesBrackett: sadly I have that assembly in play, but the class seems to live outside of that dll.Olcott

© 2022 - 2024 — McMap. All rights reserved.