So how to use the StringWriter with XmlWriter to get XML string form the writer without going through IO. You need to use some writer object to make it produce XML – but don’t need to serialize this to disk. How to do this?
- First you need a StringWriter (sw) object.
- The a XmlWriter is created with by sending the sw as input to the constructor of XmlWriter.
- Then use the XmlWriter
- Now you need to flush the sw. Call close on the StringWriter
And close the XmlWriter
- Then you get the XML from the sw.ToString method.
StringWriter sw = new StringWriter();
var writer = XmlWriter.Create(sw);
//Don't need to do this writer.Close();
Console.WriteLine("returned with sw:" + sw.ToString());