Solved

XmlNodeList remove empty elements

Posted on 2012-03-14
5
1,080 Views
Last Modified: 2012-03-15
I use this function to remove all empty elements in my xml file:
XmlNodeList emptyElements = doc.SelectNodes(@"//*[not(node())]");
for (int j = emptyElements.Count - 1; j >= 0; j--)
{
    emptyElements[j].ParentNode.RemoveChild(emptyElements[j]);
}

How can I change the script to only remove empty elements with the name category?
<category name="test" />
0
Comment
Question by:bongii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37720853
Try:

XmlNodeList emptyElements = doc.SelectNodes(@"//*[not(node()) and @name='test']");

Open in new window

0
 

Author Comment

by:bongii
ID: 37723662
Hi.
Thank you for your reply.
But it´s <category i want to remove.
There is serveal different names:
<category name="test1" />
<category name="test2" />
<category name="test2" />

So something like
XmlNodeList emptyElements = doc.SelectNodes(@"//*[not(node()) and NODENAME='category']");
0
 

Author Comment

by:bongii
ID: 37723683
I solved it by:
XmlNodeList emptyElements = doc.SelectNodes(@"//*[not(node()) and @name!='']");
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37724325
But it´s <category i want to remove
Oh. Sorry, I misread this:

How can I change the script to only remove empty elements with the name category?

as "name attribute." My apologies. Instead of the *, why not use "category"?

e.g.

XmlNodeList emptyElements = doc.SelectNodes(@"//category[not(node())]");

Open in new window

0
 

Author Comment

by:bongii
ID: 37724399
Thank you very much. It works even better
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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