[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1141
  • Last Modified:

XmlNodeList remove empty elements

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
Jesper Christensen
Asked:
Jesper Christensen
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
Try:

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

Open in new window

0
 
Jesper ChristensenProgrammerAuthor Commented:
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
 
Jesper ChristensenProgrammerAuthor Commented:
I solved it by:
XmlNodeList emptyElements = doc.SelectNodes(@"//*[not(node()) and @name!='']");
0
 
käµfm³d 👽Commented:
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
 
Jesper ChristensenProgrammerAuthor Commented:
Thank you very much. It works even better
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now