473,396 Members | 1,834 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,396 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 3374
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: Scott | last post by:
The following blog gives a neat example of how to transform directly into a node: http://blogs.msdn.com/xmlteam/ However, the code doesn't seem to work as it is presented in the blog. I have...
3
by: Eckhard Schwabe | last post by:
when switching from the old "XslTransform " to "XslCompiledTransform" I notice a difference in the handling of whitespace. I need to transform a XML file which contain tabs (\t), and which remain...
1
by: Steve | last post by:
Using VB.NET 2.0 I have a simple routine that attempts transforms an XmlDocument with an XSLT stylesheet into HTML. Under the old 1.1 framework with XslTransform, everything worked fine. Now...
4
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
5
by: Steve | last post by:
I am using XslCompiledTransform to transform XML into Excel 2003 SpreadsheetML. In order to get the resulting XML file to open in Excel, we need the following two lines at the top of the file: ...
2
by: tony | last post by:
XslCompiledTransform xslt = new XslCompiledTransform(); ASCIIEncoding encodeToByteArray = new ASCIIEncoding(); MemoryStream inputStream = new MemoryStream(); MemoryStream outputStream = new...
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) {...
3
by: Andy Fish | last post by:
Hi, From reading the documentation, I get the impression that XslCompiledTransform should be faster than XslTransform on my test with a large complex document and a large complex XSLT, the...
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... ...
1
by: =?Utf-8?B?Vmlub2Q=?= | last post by:
Hi, I am having a problem doing the transform. Actually i am migrating the code from XslTranform class into XslCompiledTransform class. Here is the code that i am using in .net 1.1 private...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.