Return XML. Please, need help. Thanks.

S

shapper

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
 
F

Flinky Wisty Pomm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top