remove an XML node with specific attribute from doc

I have a problem with my vb.net code where I need to remove a specific node with any attribute from a document where the value of the node is empty.
ie: <Inventory>
        <fruit id="apples">5</fruit>
        <fruit id="bananas"></fruit>
        <fruit id="oranges">12</fruit>
    <Inventory>
razingfoolsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

nepaluzCommented:
try this

        Dim xTree As XElement = <Inventory>
                                    <fruit id="apples">5</fruit>
                                    <fruit id="bananas"></fruit>
                                    <fruit id="oranges">12</fruit>
                                </Inventory>
        Try
            For Each x As XElement In xTree.Descendants
                If String.IsNullOrEmpty(x.Value.ToString) Then x.Remove()
            Next
        Catch ex As Exception

        End Try

Open in new window

0
razingfoolsAuthor Commented:
Thanks for the quick response!
Sorry, but I posted this when i was rather sleepy and was trying not to complicate the issue too much, but I'm actually using a namespace manager as I'm having to do quite a bit of validating  and manipulation throughout the xml.  I tried to manipulate your suggestion, but without success.
My code actually goes something like this:

Dim _nsManager As New Xml.XmlNamespaceManager(_Document.NameTable)
Try
Dim _nodeList5 As Xml.XmlNodeList  = _Document.DocumentElement.SelectNodes(".//bz:fruit [@id='bananas']", _nsManager)
                  
For Each _node As Xml.XmlNode In _nodeList5
   If Len(_node.InnerText) = 0 Then
      _node.ParentNode.RemoveChild(_node)
    End If
Next
Catch
End Try      
0
nepaluzCommented:

1. How does _nodeList5 evaluate when you debug?
2. Once inside the For Each loop, does _node evaluate to anything?

try this then

            For Each _node As Xml.XmlNode In _nodeList5
                If String.IsNullOrEmpty(_node.Value.ToString) Then
                    _node.ParentNode.RemoveChild(_node)
                End If
            Next

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
razingfoolsAuthor Commented:
Thanks for the help!  Your suggestion was spot on.
0
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
Web Components

From novice to tech pro — start learning today.