Getting an XMLWriter from an XslCompiledTransform.Transform call

S

Simon Harvey

Hi all,

I'm having a real problem with getting an XMLWriter as a result of an
xsl tranform I'm attempting.

My code is:

private void btnPerformTransform_Click(object sender, EventArgs e) {
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
XmlWriter xmlWriter = new XmlTextWriter(new StringWriter());

xslt.Load("FlightsAvail.xsl");

xslt.Transform("testXMLIn.xml", xmlWriter);

Console.Out.Write("");
}

This code is returning the following exception:

Execution of the 'document()' function was prohibited. Use the
XsltSettings.EnableDocumentFunction property to enable it.

I've tried numerous alternatives to get this to work and I'm frustrated
because I know its going to be really simple.

One thing I'd point out though, is that it doesn't really matter what
object form the output of the transform comes as - as long as I can then
load it into an XMLDocument instance without having to write the output
of the transform to a file first.

I've been trying all sorts of things with memory streams and what not,
but I keep coming up against exceptions. As you can maybe tell, I don't
do IO stuff very often.

If anyone has any code that can get me an xmlwriter or something similar
containing the results of an xsl transform I would be really grateful.

Many thanks in advance

Kindest Regards

Simon
 
M

Marc Gravell

OK - you have 2 things going on. The main one *isn't* an IO problem.

The first is that the XslCompiledTransform (as the error suggests)
*disables* certain functions by default; so if your xsl *uses* functions
like document(), then to get this to behave like you want you need to do
what the exception says: when you call Load, you must use the overload that
accepts an options object, to which you should enable this mode.

However, related to getting the writer... at the moment you are transforming
into an xmlwriter that is going to write to a stringwriter, which *you don't
have a reference to*, so you can't actually get the results.

You need to do something like
StringWriter sw = new StringWriter();
XmlWriter xmlWriter = new XmlTextWriter(sw);
xslt.Transform(path, xmlWriter);
string xml = sw.ToString();

(note if you lose trailing data you may need to Flush(), Close() or
Dispose() the xmlWriter before calling sw.ToString()).

Marc
 
S

Simon Harvey

Marc said:
OK - you have 2 things going on. The main one *isn't* an IO problem.

The first is that the XslCompiledTransform (as the error suggests)
*disables* certain functions by default; so if your xsl *uses* functions
like document(), then to get this to behave like you want you need to do
what the exception says: when you call Load, you must use the overload that
accepts an options object, to which you should enable this mode.

However, related to getting the writer... at the moment you are transforming
into an xmlwriter that is going to write to a stringwriter, which *you don't
have a reference to*, so you can't actually get the results.

You need to do something like
StringWriter sw = new StringWriter();
XmlWriter xmlWriter = new XmlTextWriter(sw);
xslt.Transform(path, xmlWriter);
string xml = sw.ToString();

(note if you lose trailing data you may need to Flush(), Close() or
Dispose() the xmlWriter before calling sw.ToString()).

Marc

Hi Marc,

I've got it working now.

Huge thank you to you for the advice

Kindest Regards

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top