473,424 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 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 8794
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sohan Kamat | last post by:
Hi, We are using XSLCompiledTransform.Transform for Transforming XML to a text file. One of the XML tags has value <Address>Mahin & Mahin</Address> And in the XSL we have <xsl:value of...
4
by: benben | last post by:
I have the following code snippet: class converter { // ... public: virtual polar_point convert_to_polar(const point&) = 0; }; void f(const std::vector<point>& points, converter& cvt)
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
5
by: Kevin Burton | last post by:
I am trying an XSLT transformation using the XslCompiledTransform class. I get an exception when I call the Transform method. It seems to point to these two lines: <xsl:namespace...
2
by: Simon Harvey | last post by:
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) {...
1
by: Mark | last post by:
I'm getting the error "The URI scheme is too long." in the code below. What is URI in this context? The XSL or the XML being transformed? Thanks! -Mark StringBuilder sb = new...
3
by: wolverine | last post by:
Hi, I am injecting a javascript code into html pages and attaching some dom events to some elements via attachEvent (IE only). I just want to know that is there any chance by which my event...
5
by: =?Utf-8?B?Um9iZXJ0?= | last post by:
Hello all, I have an XmlException being thrown trying to use XslCompiledTransform while using the google api. My suspicion is there is some html decoding happening that I want to prevent... ...
4
by: jinendrashankar | last post by:
Hi In my Code i am creating 2 object of my class and in assigentment operator passing arugement by reference. In output of my code i am getting to 2 Constructor call which right but there is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.