Formatting an XML string in C#

Here’s a nifty trick if you’ve got a chunk of XML you want to format nicely:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlStringVariable);

MemoryStream memoryStream = new MemoryStream(10240);
xmldoc.Save(memoryStream);
string formattedXml = Encoding.ASCII.GetString(memoryStream.GetBuffer());
memoryStream.Close();
Posted on 05 Nov 04 by Helen Emerson (last updated on 05 Nov 04).
Filed under .NET