By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

Return XML. Please, need help. Thanks.

P: n/a
Hello,

For the past hours I have been trying to solve a problem which is
driving me crazy.

I have to different codes where the problem to solve is the same:

CODE 1 (Transforms a XML document using a XSL file):

Function Trans()
Dim doc As XmlDocument = New XmlDocument
doc.Load("doc.xml")
Dim docXsl As XslCompiledTransform = New XslCompiledTransform
docXsl.Load("doc.xsl")
docXsl.Transform(doc, Nothing,
HttpContext.Current.Response.Output)
End Function
CODE 2 (Creates XML)

Function Create()
Dim doc As XmlTextWriter = New
XmlTextWriter(Response.OutputStream, Encoding.UTF8)
...
doc.WriteStartElement("item")
doc.WriteElementString("title", "My Title")
doc.WriteEndElement()
...
doc.Flush()
doc.Close()
End Function
I don't want to display the XML files. I don't want to save the XML
files.

I want to return the XML created from each function.
Then outside of each function I want to display the XML in the browser.

Yes, I read about stream or using a string.
But I was not able to make this work.

Could someone please help me out?
Please, give me a code example.

Thank You Very Much,
Miguel

Oct 31 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You're currently writing the transformed Xml to the Response stream.
The easiest change to your code is (forgive VB.Net typos, I'm a C# guy)

Function Trans() As Stream
Dim outputStream As Stream = New MemoryStream
Dim doc As XmlDocument = New XmlDocument
Dim docXsl As XslCompiledTransform = New XslCompiledTransform

' Grab and transform the data into the stream
doc.Load("doc.xml")
docXsl.Load("doc.xsl")
docXsl.Transform(doc, Nothing, outputStream)

'Set the stream back to the beginning
outputStream.Seek(0,0)
Return outputStream
End Function

And now you're returning a MemoryStream containing your transformed
xml. You can read the data with an XmlReader, flush it to a file or
database, or write it directly to your HttpResponse.

shapper wrote:
Hello,

For the past hours I have been trying to solve a problem which is
driving me crazy.

I have to different codes where the problem to solve is the same:

CODE 1 (Transforms a XML document using a XSL file):

Function Trans()
Dim doc As XmlDocument = New XmlDocument
doc.Load("doc.xml")
Dim docXsl As XslCompiledTransform = New XslCompiledTransform
docXsl.Load("doc.xsl")
docXsl.Transform(doc, Nothing,
HttpContext.Current.Response.Output)
End Function
CODE 2 (Creates XML)

Function Create()
Dim doc As XmlTextWriter = New
XmlTextWriter(Response.OutputStream, Encoding.UTF8)
...
doc.WriteStartElement("item")
doc.WriteElementString("title", "My Title")
doc.WriteEndElement()
...
doc.Flush()
doc.Close()
End Function
I don't want to display the XML files. I don't want to save the XML
files.

I want to return the XML created from each function.
Then outside of each function I want to display the XML in the browser.

Yes, I read about stream or using a string.
But I was not able to make this work.

Could someone please help me out?
Please, give me a code example.

Thank You Very Much,
Miguel
Oct 31 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.