Solved

XmlNodeList remove empty elements

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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