Help with deleting data elements in xml file

Hi,

I'm trying to delete all ldata elements in my xml file except for the first data element which LinkAID is 0. But I'm getting error message:


Expression must evaluate to a node-set.

On line:

   For Each topping As Xml.XmlNode In piz.SelectNodes("/Root/LinkA[LinkAID <> '" & 0 & "']")


How do I fix this error?

Code:
 Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
        Dim dk As New XmlDocument
        dk.Load(Application.StartupPath + "\LinkSSADB.xml")
        Dim piz As XmlElement = dk.DocumentElement
        For Each topping As Xml.XmlNode In piz.SelectNodes("/Root/LinkA[LinkAID <> '" & 0 & "']")
            topping.ParentNode.RemoveChild(topping)
            dk.Save(Application.StartupPath + "\LinkSSADB.xml")
            Exit For
        Next

Thanks,

Victor
vcharlesAsked:
Who is Participating?
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.

SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi... you can delete element from xml file through this example. Please go through this example

Dim doc As New XmlDocument()
Dim nodes As XmlNodeList
doc.Load("c:\temp\node.xml")
nodes = doc.SelectNodes("/group/Second_group")

For Each node As XmlNode In nodes
    If node IsNot Nothing Then
        node.RemoveAll()
          doc.Save("c:\temp\node.xml")
    End If
Next

Or LINQ to XML:

Dim doc As XDocument = XDocument.Load("c:\temp\node.xml")
doc.Root.Element("Second_group").Elements("Name").Remove()
doc.Save("c:\temp\node.xml")
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
vcharlesAuthor Commented:
Hi,

I don't want to remove all the elements only the ones where LinkAID <> 0, which is the first element. For example if I have the xml below in Part A I only want to end up with the file in Part B.

Part A:

<Root>
<Table>
<LinkAID>0<LinkAID>
<Item1></Item1>
<Item2></Item2>
<Item3></Item3>
</Table>
<Table>
<LinkAID>1<LinkAID>
<Item1></Item1>
<Item2></Item2>
<Item3></Item3>
</Table>
<Table>
<LinkAID>2<LinkAID>
<Item1></Item1>
<Item2></Item2>
<Item3></Item3>
</Table>
<Table>
<LinkAID>3<LinkAID>
<Item1></Item1>
<Item2></Item2>
<Item3></Item3>
</Table>
</Root>


Part B:
<Root>
<Table>
<LinkAID<0<LinkAID>
<Item1></Item1>
<Item2></Item2>
<Item3></Item3>
</Table>
</Root>

Thanks,

V.
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
Visual Basic.NET

From novice to tech pro — start learning today.