Elegant way to remove all nodes from XML whose child has text that ISNOT in array

J

jsmith

Hi all,

I need help on an elegant way of removing nodes from XML that do not
have children with text that is kept in array.
Here is the example:

I have array of integers = {123, 456}
and an XML like this:
<W>
<U></U>
<P></P>
<S></S>
<LIST>
<INS>
<I>
<L></L>
<F></F>
<ID>123</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>456</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>999</ID>
</I>
</INS>
</LIST>
</W>

I need to remove all <INS> nodes whose I/ID child has text that IS NOT
in array. In here it is the last <INS> node because ID text is 999
which is not in array.

Thanks
 
M

Martin Honnen

jsmith said:
I need help on an elegant way of removing nodes from XML that do not
have children with text that is kept in array.

With .NET 3.5 you can use LINQ to XML as follows:

Dim el As XElement = _
<W>
<U></U>
<P></P>
<S></S>
<LIST>
<INS>
<I>
<L></L>
<F></F>
<ID>123</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>456</ID>
</I>
</INS>
<INS>
<I>
<L></L>
<F></F>
<ID>999</ID>
</I>
</INS>
</LIST>
</W>

Dim a As Integer() = {123, 456}

el...<INS>.Where(Function(ins) Not
(a.Contains(ins.<I>.<ID>.Value))).Remove()

'Save to Console for testing
el.Save(Console.Out)


If you can't use LINQ to XML then try

Dim doc As New XmlDocument()
doc.Load("..\..\XMLFile1.xml")

Dim a As Integer() = {123, 456}

For Each ins As XmlElement In doc.SelectNodes("/W/LIST/INS")
If Not (a.Contains(ins("I")("ID").InnerText)) Then
ins.ParentNode.RemoveChild(ins)
End If
Next

doc.Save(Console.Out)

that should work with .NET 1.0 and later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top