Link to home
Start Free TrialLog in
Avatar of sybe
sybe

asked on

Get XSL-string back from XslCompiledTransform

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?


Avatar of abel
abel
Flag of Netherlands image

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

Avatar of sybe
sybe

ASKER

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


> 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

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sybe

ASKER

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.


Avatar of sybe

ASKER

Thanks!
> 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!