all groups > dotnet xml > june 2007 >
You're in the

dotnet xml

group:

XslCompiledTransform.Transform() method question


XslCompiledTransform.Transform() method question Jeff
6/26/2007 8:45:01 AM
dotnet xml:
VS2005; .NET 2.0

I want to use the XslCompiledTransform.Transform() method for its stated
purposes.

However, I have an XmlDocument object already in memory and I can't figure
out how to use that without first saving it to disk and then referencing that
file in the Transform() method.

xslt.Load("MyXSLfile.xslt");
XMLdoc.Save("XSLTtransform.xml");
xslt.Transform("XSLTtransform.xml", "XSLTtransform.htm");
String XMLtransform = File.ReadAllText("XSLTtransform.htm");

Now I have my transformed XML into HTML string and can put it in my current
HTMLDocument. But surely there is some way to do this without reading/writing
to actual files. I just can't figure this out.

Re: XslCompiledTransform.Transform() method question Jeff
6/26/2007 9:39:01 AM
Hi Martin,

And thanks for your timely info.

Unfortunately, I don't understand any of that syntax or the use of the
Readers/Writers. I read the Online VS2005 help doc for this class - and I
don't get it. So, your response sounds like that doc to me. A concrete
example would go miles towards helping my mental deficit. :)

I guess what I am asking is if my code can be rewritten without using
external files. If so, a concrete example replacing mine would help me to
understand. I seem to have some mental block.

Thanks much if you are able to provide such an example.

Re: XslCompiledTransform.Transform() method question Jeff
6/26/2007 2:30:01 PM
Hi Martin,

One step closer it appears. But aren't you still using a File for the
translated XML? e.g. "result.html"

Can't I get it back in a String?

Thanks

[quoted text, click to view]
Re: XslCompiledTransform.Transform() method question Jeff
6/26/2007 2:55:03 PM
Thanks Martin.

With your example, I was able to get my base understanding of readers/writers.

Here is what worked for me.

StringWriter sw = new StringWriter();
xslt.Load("MyXSLfile.xslt");
xslt.Transform(XmlDoc.CreateNavigator(), null, sw);
// now sw.ToString() has the contents of the translated XML
Re: XslCompiledTransform.Transform() method question Martin Honnen
6/26/2007 5:59:29 PM
[quoted text, click to view]

There are several overloads of the Transform method that take an
IXPathNavigable as the first argument, for instance this one:
<http://msdn2.microsoft.com/en-us/library/ateytk4a.aspx>
So you can call
xslCompiledTransformInstance(xmlDocumentInstance.CreateNavigator(), ...)
for those methods.

Then there are overloads taking an XmlReader as the first argument so
you can pass in an XmlNodeReader over your XmlDocument
xslCompiledTransformInstance(New XmlNodeReader(xmlDocumentInstance), ...)


--

Martin Honnen --- MVP XML
Re: XslCompiledTransform.Transform() method question Martin Honnen
6/26/2007 7:10:51 PM
[quoted text, click to view]

Here is an example creating an IXPathNavigable using CreateNavigator,
assuming you have an XmlDocument variable named yourXmlDocumentInstance:

Dim xsltProcessor As New XslCompiledTransform()
xsltProcessor.Load("sheet.xslt")
xsltProcessor.Transform(_
yourXmlDocumentInstance.CreateNavigator(), _
Nothing, _
File.Open("result.html", FileMode.Create))

I posted the wrong link somehow, the link to the method overload should be
<http://msdn2.microsoft.com/en-us/library/ms163434.aspx>


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button