Get XSL-string back from XslCompiledTransform

sybe
sybe used Ask the Experts™
on
Since I seem to be asking question that are hard to answer, here is another one.

It is easy to create an XslCompiledTransform from a file.

    Dim oXSL As New XslCompiledTransform()
    oXSL.Load("c:\temp\file.xsl")

The question is: can I get the XSL-string back from the XslCompiledTransform object. Or do I need to read the file from disk again?


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
I'd actually do it the other way around: load the file from disk in a stream, read it in a string create XML from that string and give the XML DOM object to the XSL.Load method (as XmlReader). The string can then be used for anything you like. Something like this:

Dim strXslt As String = ""
Using reader As New XmlTextReader("c:\temp\temp.xsl")
    strXslt = reader.ReadString()
End Using
 
Dim doc As New XmlDocument()
doc.LoadXml(strXslt)
Dim compiledXsl As New XslCompiledTransform()
compiledXsl.Load(doc)

Open in new window

Author

Commented:
Because there seems no way to get the XSL string back from the XslCompiledTransform, that is what I am doing now.

More or less that is, I skip the XmlTextReader, because XmlDocument can read from disk directly:

Dim oXML As New XmlDocument()
oXML.Load("C:\temp\file.xsl")


Interestingly enough you are using an undocumented method

XslCompiledTransform.Load() is not documented to take XMLDocument as parameter, but it works anyway.

http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.load.aspx


Top Expert 2009

Commented:
> because XmlDocument can read from disk directly:
What do you mean with "directly"? It internally uses the XmlTextReader. See snippet from XmlDocument's code below. Meaning, you can skip it, but you won't because it is used anyway (which means: skipping is the easier way to code, of course).

> Interestingly enough you are using an undocumented method
Not really ;-) It is documented here: http://msdn.microsoft.com/en-us/library/ms163422.aspx

public virtual void Load(string filename)
{
    XmlTextReader reader = this.SetupReader(new XmlTextReader(filename, this.NameTable));
    try
    {
        this.Load(reader);
    }
    finally
    {
        reader.Close();
    }
}
 
 
 
 

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Top Expert 2009
Commented:
> Because there seems no way to get the XSL string back from the XslCompiledTransform, that is what I am doing now.

I wouldn't know why you want to do that, because you have that string at your fingertips (and: it really is not a string when looked at from the XslCompiledTransform! ;)

If you look through the code and the way the URL is loaded as file into the XslCompiledTransform before it is being used, there's not a single moment it is changed into a string. And probably with a reason: strings are slow, and it is already very hard to make a quick XSLT parser. And Microsoft's XSLT parser is just about the fastest around at the moment (varies depending on type of transform).

Internally, the DOM is not stored. Instead, the XML is read as DOM and immediately dismissed as soon as it is compiled into internal IL code (XmlILCommand). This is also a reason why the XSLT file, once loaded, is immutable: it doesn't exist anymore. All for the favor of performance (basically: all known XSLT processors follow a similar path).


Author

Commented:
Well, I wanted to do that because I need to adapt the XSL after loading it. It is really nice that XslCompiledTransform is fast, but it is also quite frigid. So it should be used only after adapting the XSL.

Concerning your earlier remarks about skipping XmlReader and XmlDocument as parameter for XslCompiledTransform.Load()

I see. You are right.

Anyway, you earn all credits for explaining. Thanks.


Author

Commented:
Thanks!
Top Expert 2009

Commented:
> but it is also quite frigid
very much so indeed. You can always write your own wrapper to make it a bit easier to do all these things, and just set some properties the way you want it, including the URI to your XSLT file. Then you simply make one Transform method, which applies these settings and runs the transform.

But I'm sure you know how to do that ;-)

tx for the grade and the follow-up!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial