views:

2302

answers:

3

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?

+6  A: 

If you really want to write into memory, pass in a StringWriter or a StringBuilder like this:

using System;
using System.Text;
using System.Xml;

public class Test
{
    static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;        
        StringBuilder builder = new StringBuilder();

        using (XmlWriter writer = XmlWriter.Create(builder, settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            writer.WriteStartElement("element");
            writer.WriteString("content");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
        Console.WriteLine(builder);
    }
}

If you want to write it directly to the response, however, you could pass in HttpResponse.Output which is a TextWriter instead:

using (XmlWriter writer = XmlWriter.Create(Response.Output, settings))
{
    // Write into it here
}
Jon Skeet
+2  A: 

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.

Steve
I suggest writing to Output rather than OutputStream. Why bother to build a StreamWriter around a Stream when Output already provides that?
Jon Skeet
In this case, I would agree you're probably right. I say that because judging by the provided snippet, he's already got the whole document stored as a string (returnXML), so the easiest thing is to just write that string to Response.Output and avoid XmlWriters entirely.
Steve
No, returnXml was what we was hoping to pass *into* the XmlWriter.Create call. I suspect he can just get away with XmlWriter.Create(Response.Output, settings)
Jon Skeet
I passed in context.Response.OutputStream and set context.Response.Buffer = true and then just called .Flush() at the end, thanks guys for the guidance!
shogun
A: 
    StringBuilder xml = new StringBuilder();
    TextWriter textWriter = new StringWriter(xml);
    XmlWriter xmlWriter = new XmlTextWriter(textWriter);

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:

    StringBuilder xml = new StringBuilder();
    XmlWriter xmlWriter = XmlWriter.Create(xml);
BFree
You don't need to use the StringWriter here - XmlWriter.Create accepts a StringBuilder directly.
Jon Skeet
Oh cool, you're right. I never noticed that. Thanks. I did check Reflector now though, and as I suspected, internally a StringWriter is created. Thanks for the input! I'll edit my answer.
BFree