472,145 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Transform to a Stream or XmlReader?

Hi,

I have this code that will write the transformed XML immediately to the browser with the Response object..

XslTransform trans = new XslTransform()
trans.Load(MapPath("MyXsl.xsl"))
trans.Transform(oXmlDataDocument, null, Response.OutputStream, null); //Writes immediately to browser her

But if I wanted to return the transformation as a variable and not immediately to the browser, the only way I found to do this was using the XmlReader below..

XmlUrlResolver resolver = new XmlUrlResolver()
XmlReader reader = trans.Transform(oXmlDataDocument, null, resolver)
while (reader.Read())
//Build the XML string here...
Is the XmlReader considered efficient enough or is there another "Stream" type object that I can use instead of the Response.OutputStream and just "flush" to the browser when I'm ready

I've tried declaring a StreamReader or Stream but it wants parameters in the constructor

Any code on this would be helpful. Dave.
Nov 12 '05 #1
5 4666
The XmlReader is very efficient if you want to parse the resulting Xml. If
you just want to write it to a string then you might want to consider
transforming to a StringWriter.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Dave" <an*******@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
Hi,

I have this code that will write the transformed XML immediately to the browser with the Response object...
XslTransform trans = new XslTransform();
trans.Load(MapPath("MyXsl.xsl"));
trans.Transform(oXmlDataDocument, null, Response.OutputStream, null); //Writes immediately to browser here
But if I wanted to return the transformation as a variable and not immediately to the browser, the only way I found to do this was using the
XmlReader below...
XmlUrlResolver resolver = new XmlUrlResolver();
XmlReader reader = trans.Transform(oXmlDataDocument, null, resolver);
while (reader.Read()) {
//Build the XML string here....
}

Is the XmlReader considered efficient enough or is there another "Stream" type object that I can use instead of the Response.OutputStream and just
"flush" to the browser when I'm ready?
I've tried declaring a StreamReader or Stream but it wants parameters in the constructor.
Any code on this would be helpful. Dave.

Nov 12 '05 #2
Dave wrote:

Is the XmlReader considered efficient enough or is there another "Stream" type object that I can use instead of the Response.OutputStream and just "flush" to the browser when I'm ready?


What's wrong with transforming directly to Response.OutputStream?
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #3
Thanks, I guess I'm missing something. I'm looking at the XslTransform.Transform overloads but not seeing how you can write to a StringWriter.

Basically, I'm trying to perform a XSL Transform in a Web Service to reformat the XML and make it more readible but I don't know how to return it. I've tried the Transform method to return an XmlReader

XmlReader reader = trans.Transform(myXmlDataDocument, null, resolver);

Then build and return the string out of the reader, but I lose the encoding when I try to run the webservice and display it in the browser. "<" comes back as "&lt;".

Thanks, Dave.
Nov 12 '05 #4
Oleg

Thanks, that seems to be the way to go instead of of all other stuff the DataSet returns. Thanks, Dave.
Nov 12 '05 #5
StringWriter derives from TextWriter, hence you can say:

StringWriter writer = new StringWriter();
string transformedXml = null;
trans.Transform(myXmlDataDocument, null, writer);

transformedXml = writer.ToString();

Also see [0] in the framework SDK docs on your local drive for a full
example.

Keep in mind that string objects always store their content with UTF-16
character encoding. If you need something different you have to write to a
MemoryStream.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0]
ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemxmlxslxsltransformcla
sstransformtopic14.htm
"Dave" <an*******@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
Thanks, I guess I'm missing something. I'm looking at the XslTransform.Transform overloads but not seeing how you can write to a
StringWriter.
Basically, I'm trying to perform a XSL Transform in a Web Service to reformat the XML and make it more readible but I don't know how to return
it. I've tried the Transform method to return an XmlReader
XmlReader reader = trans.Transform(myXmlDataDocument, null, resolver);

Then build and return the string out of the reader, but I lose the encoding when I try to run the webservice and display it in the browser.
"<" comes back as "&lt;".
Thanks, Dave.

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by B Johnson | last post: by
6 posts views Thread by Stephen Cook | last post: by
1 post views Thread by Geoffrey Gallaway | last post: by
6 posts views Thread by Rob Meade | last post: by
reply views Thread by K. Wilder | last post: by
6 posts views Thread by Gina_Marano | last post: by
reply views Thread by Saiars | 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.