Search code examples
c#.netxmlxmldocument

Remove XML tags with empty attribute in C#


I am looking for a good approach that can remove empty tags as well as all tags without any attribute from XML efficiently.

For example, Consider the following Sample xml file

  <?xml version="1.0"?>
<Root xmlns:xsd="" xmlns:xsi="" name="">
  <Branches>
    <Branch name="TEST">     
      <Branches>
    <parametrs/>
    <Branch name="abc"/>
        <Branch name="Subtest">
          <Branches>
            <Branch name="sample">      
            </Branch>
          </Branches>
        </Branch>
 </Branches>
  </Branch>    
</Branches>
<Branches>
    <Branch name="TEST1">
      <Branches>
        <Branch name="Subtest">
          <Branches>
            <Branch name="sample">      
            </Branch>
          </Branches>
        </Branch>
 </Branches>
  </Branch>    
</Branches> 
</Root>

Could Become:

<?xml version="1.0"?>
<Root xmlns:xsd="" xmlns:xsi="" name="">
<Branch name="TEST">     
    <Branch name="abc"/>
        <Branch name="Subtest">   
            <Branch name="sample"/>        
        </Branch>
</Branch>    
<Branch name="TEST1">  
  <Branch name="Subtest">
      <Branch name="sample"/>         
  </Branch>
</Branch>    
</Root>

Any help is greatly appreciated.


Solution

  • You could do the following (comments in code).

    var xdoc = XDocument.Parse(xml);
    var nodesToRemove = new List<XElement>();
    
    // Get the List of XElements that do not have any attributes and iterate them
    foreach(var node in xdoc.Descendants().Where(e => !e.HasAttributes))
    {
        // If any XElement has children, move them up
        if(node.Elements().Any())
        {
            node.Parent.Add(node.Elements());
    
        }
        // Mark the node for removal
        nodesToRemove.Add(node);
    }
    
    // Remove the marked nodes
    foreach(var item in nodesToRemove)
    {   
        item.Remove();
    }
    
    var resultXml = xdoc.ToString();