472,119 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Getting an XMLWriter from an XslCompiledTransform.Transform call

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
Dec 8 '06 #1
2 8722
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
Dec 8 '06 #2
Marc Gravell wrote:
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
Dec 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sohan Kamat | last post: by
4 posts views Thread by benben | last post: by
5 posts views Thread by Kevin Burton | last post: by
1 post views Thread by Mark | last post: by
5 posts views Thread by =?Utf-8?B?Um9iZXJ0?= | last post: by
4 posts views Thread by jinendrashankar | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.