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
}