Trouble using XmlTextWriter with MemoryStreams
-
06-07-2019 - |
Question
I'm having trouble getting a memory stream and a XML text writer class to work together properly.
Context.Reponse.BufferOutput=true;
Context.Response.ContentType = "application/xml";
stmOutput = new MemoryStream();
Output = new XmlTextWriter(stmOutput, Encoding.ASCII);
Output.WriteStartDocument();
Output.WriteStartElement("MyTag");
Output.WriteEndElement();
Output.WriteEndDocument();
Output.Flush();
stmOutput.Flush();
Context.Response.OutputStream.Write(stmOutput.ToArray(),0,(int)stmOutput.Length-1);
Context.Response.OutputStream.Flush();
Output.Close();
This is being done inside an ASHX file. When I run this, all that gets output is
<?xml version="1.0" encoding="us-ascii"?><MyTag /
Am I missing something with Response, or the memory stream or the XML text writer? I'm kinda lost because I'm following a little guide to it doing everything it shows but instead using a memory stream, and its not working. .
Solution
Have you tried to remove the -1
in Length
? I think the only thing missing in the output is a ">"
.
Context.Response.OutputStream.Write(stmOutput.ToArray(), 0, (int)stmOutput.Length);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow