I am creating an ASHX that returns XML however it expects a path when I do
XmlWriter writer = XmlWriter.Create(returnXML, settings)
But returnXML is just an empty string right now (guess that won't work), however I need to write the XML to something that I can then send as the response text. I tried XmlDocument but it gave me an error expecting a string. What am I missing here?
Something was missing on my side: flushing the XmlWriter's buffer:
Then, use the xmlWriter to do all the xml writing, and that writes it directly to the StringBuilder.
Edit: Thanks to Jon Skeet's comment:
If you really want to write into memory, pass in a
StringWriter
or aStringBuilder
like this:If you want to write it directly to the response, however, you could pass in
HttpResponse.Output
which is aTextWriter
instead:The best way to do that is to write directly to the Response Output Stream. Its a stream that's built-in to ASP.NET to allow you to write whatever output as a stream, in this case you can write XML to it.