DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Remove Empty XML Nodes

05.15.2007
| 7430 views |
  • submit to reddit
        Remove nodes like <node1/> (without attributes & without children)
public static void RemoveEmptyNodes(XmlDocument doc)
{
  XmlNodeList nodes = doc.SelectNodes("//node()");
            
  foreach (XmlNode node in nodes)
    if ((node.Attributes.Count == 0) && (node.ChildNodes.Count == 0))
      node.ParentNode.RemoveChild(node);
}