Removing unused elements from the XML

Posted on 2014-08-14
Last Modified: 2014-08-23
Dear all

i am working with VS 2010, C#
i created my XML and XSD then i created the class file.

after serializing the class to XML
i found something like this:

in my original XML

i have this tag:

<StatusCode>Status Code (ex:0200)</StatusCode>
<Severity>Severity (Enum)</Severity>
<StatusDesc>Fawry Status Message Description</StatusDesc>

if i didnt fill the elements of the tag then it appear like "<Status/>"

i want to remove it.

how can i remove unused elements.

Question by:RamzyNEbeid
    LVL 9

    Assisted Solution

    Here is a sample that may do what you need.
                System.Xml.XmlDocument xDoc = new XmlDocument();
                XmlNodeList xList = xDoc.SelectNodes("//*");
                foreach (XmlNode xNode in xList)
                    string sName = xNode.Name;
                    if (!(xNode is XmlElement))
                    XmlElement xElement = (XmlElement)xNode;
                    if (xElement.ChildNodes.Count > 0 || xElement.Attributes.Count > 0)
                    if (xElement.InnerText.Length > 0)
                string sXml = xDoc.OuterXml;

    Open in new window

    LVL 62

    Accepted Solution

    Hi RamzyNEbeid;

    The following code snippet will remove all nodes in the document that has no child elements, has no attributes and is empty from the xdoc object.

    using System.Xml.Linq;
    // Load the XML document into memory
    var xdoc = XDocument.Load("Path-To-XML-File/Status.xml");
    // Get a list of nodes to be removed from the document
    var results = (from node in xdoc.Root.Descendants()
                   where !node.HasElements && !node.HasAttributes && node.IsEmpty
                   select node).ToList();
    // Remove the node from the xdoc object.
    results.ForEach( n => n.Remove());
    // Save the modified document back to the file system

    Open in new window


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now