Search code examples
c#xmlnodesparent-child

Remove all parent node's content if any child node has no value


I want to remove the parent node if any of its children have no value.
I've tried several methods and nothing works. Remove "S_Industry_Code" parent and all its child nodes if child node "E_Code_List_Qualifier" or "E_Industry_Code is blank. I want a generic case so the code can be used in other empty parent or child nodes too. Please help! Any suggestions?

namespace TEST
{
    class Remove_Empty_Tags
    {    
        public static void Main()
        {    
            XmlDocument doc = new XmlDocument();               
            doc.Load(@"C:\Users\TestDLMS\out\I_636806391809983753.xml");    
            new RemoveNulls().RemoveEmptyNodes(doc);    
            doc.Save(@"C:\users\Desktop\AllNullsRemoved.xml");    
        }    
    }
}   

   class RemoveNulls
   {
        public void RemoveEmptyNodes(XmlDocument doc)
        {
            XmlNodeList nodes = doc.SelectNodes("//node()");
            foreach (XmlNode node in nodes)
                if ((node.Attributes != null && node.Attributes.Count == 0) && (node.ChildNodes != null && node.ChildNodes.Count == 0))
                {
                  //  node.ParentNode.RemoveChild(node); //removes only nodes that are blank
                      node.ParentNode.RemoveAll(); //removes child nodes leaves empty parent 
                  //  node.ParentNode.ParentNode.RemoveAll(); //removes everything
                }
       }
   }
// Content of XML file
<File>
  <T_Requisition_511R Standard="X12">
    <S_Transaction_Set_Header>
      <E_Transaction_Set_Code>511</E_Transaction_Set_Code>       
      <E_Transaction_Set_Number>0001</E_Transaction_Set_Number>
    </S_Transaction_Set_Header>
    <S_Beginning_Segment_for_Material_Management>
      <E_Transaction_Set_Purpose_Code>00</E_Transaction_Set_Purpose_Code>
      <E_Transaction_Type_Code>A0</E_Transaction_Type_Code>
      <E_Date>20181217</E_Date>
      <E_Time>152620</E_Time>
    </S_Beginning_Segment_for_Material_Management>
    <L_Assigned_Number>
      <L_Code_Source_Information>
        <S_Industry_Code>
          <E_Code_List_Qualifier_Code>A9</E_Code_List_Qualifier_Code>
          <E_Industry_Code>    </E_Industry_Code>
        </S_Industry_Code>
        <S_Industry_Code>
          <E_Code_List_Qualifier_Code>79</E_Code_List_Qualifier_Code>
          <E_Industry_Code>03</E_Industry_Code>
        </S_Industry_Code>
        <S_Industry_Code>
          <E_Code_List_Qualifier_Code>80</E_Code_List_Qualifier_Code>
          <E_Industry_Code>   </E_Industry_Code>
        </S_Industry_Code>
      </L_Code_Source_Information>
    </L_Assigned_Number>
  </T_Requisition_511R>
</File>

Solution

  • string xml_string = @"<File>...</File>";
    var xml = XElement.Parse(xml_string);
    var empty = xml.Descendants()
                .Where(d => d.Elements().Count() == 0 && d.Value.Trim() == "")
                .Select(d => d.Parent);
    empty.Remove();