How to collapse empty xml tags?
I get the XML as follows:
<Items>
<Row attr1="val"></Row>
<Row attr1="val2"></Row>
</Items>
This is the correct XML, as you know, but the other library I use is disabled, and it will only accept XML in this format:
<Items>
<Row attr1="val"/>
<Row attr1="val2"/>
</Items>
I already read XML in XmlDocuments, manipulating them and rewriting them with XmlWriter (), what is the easiest (and most efficient) way to "collapse" these empty tags?
:
private static void FormatEmptyNodes(XmlNode rootNode)
{
foreach (XmlNode childNode in rootNode.ChildNodes)
{
FormatEmptyNodes(childNode);
if(childNode is XmlElement)
{
XmlElement element = (XmlElement) childNode;
if (string.IsNullOrEmpty(element.InnerText)) element.IsEmpty = true;
}
}
}
:
var doc = new XmlDocument();
doc.Load(inputFilePath);
FormatEmptyNodes(doc);
doc.Save(outputFilePath);