Here is a function more or less exactly as I found it from somewhere
on the internet.
static string BeautifyXML(string sXML)
{
string result = "";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(ms,
System.Text.Encoding.Unicode);
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
try
{
//load the xml into the document object
d.LoadXml(sXML);
w.Formatting = System.Xml.Formatting.Indented;
//copy the xml into a formatting XmlTextWriter
d.WriteContentTo(w);
w.Flush();
ms.Flush();
//rewind the memory stream before reading from it
ms.Position = 0;
//extract the formatted text
result = new System.IO.StreamReader(ms).ReadToEnd();
}
catch (System.Xml.XmlException)
{
}
finally
{
try {ms.Close();} catch (Exception) {}
try {w.Close();} catch (Exception) {}
}
return result;
}
It works pretty well, most of the time. Given this string:
"<root><childNode1><deepestNode>I am the deepest
node!</deepestNode></childNode1><childNode2><deepestNode>I am also
quite deep!</deepestNode></childNode2></root>"
It produces this nicely formatted output:
<root>
<childNode1>
<deepestNode>I am the deepest node!</deepestNode>
</childNode1>
<childNode2>
<deepestNode>I am also quite deep!</deepestNode>
</childNode2>
</root>
But when a child node has a value and also has a child node of its
own, as childNode1 does here:
"<root><childNode1>I have a value<deepestNode>I am the deepest
node!</deepestNode></childNode1><childNode2><deepestNode>I am also
quite deep!</deepestNode></childNode2></root>"
This is the unfortunate result (deepestNode is inline with
childNode1):
<root>
<childNode1>I have a value<deepestNode>I am the deepest
node!</deepestNode></childNode1>
<childNode2>
<deepestNode>I am also quite deep!</deepestNode>
</childNode2>
</root>
I had expected this:
<root>
<childNode1>I have a value
<deepestNode>I am the deepest node!</deepestNode>
</childNode1>
<childNode2>
<deepestNode>I am also quite deep!</deepestNode>
</childNode2>
</root>
Is the unexpected inlining of childNode1's child a bug in the
XmlTextWriter? Is there a fix or workaround?