Removing unused elements from the XML

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:

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

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.

thanks,
RamzyNEbeidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

0
Fernando SotoRetiredCommented:
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
xdoc.Save("Path-To-XML-File/Status-Modified.xml");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.