There are times when you need to display an XML string to be able to view in a good format.


// Pretty XML format with consistant indentation.

public static String prettyXMLPrint(String XML)
{
	String Result = "";

	MemoryStream msStream =  new MemoryStream();
	XmlTextWriter xmlWriter = new XmlTextWriter(msStream , Encoding.Unicode);
	XmlDocument xmlDocument   = new XmlDocument();

	try
	{
		// Load the XmlDocument with the XML.
		xmlDocument.LoadXml(XML);

		xmlWriter.Formatting = Formatting.Indented;

		// Write the XML into a formatting XmlTextWriter
		xmlDocument.WriteContentTo(xmlWriter );
		xmlWriter.Flush();
		msStream.Flush();

		// Have to rewind the MemoryStream in order to read
		// its contents.
		msStream.Position = 0;

		// Read MemoryStream contents into a StreamReader.
		StreamReader sReader = new StreamReader(msStream);

		// Extract the text from the StreamReader.
		String FormattedXML = sReader .ReadToEnd();

		Result = FormattedXML;
	}
	catch (XmlException)
	{
	}

	msStream.Close();
	xmlWriter .Close();

	return Result;
}