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
Solved

XmlNodeList remove empty elements

Posted on 2012-03-14
5
1,076 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

809 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