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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jsmith

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

    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 <INSnodes whose I/ID child has text that IS NOT
    in array. In here it is the last <INSnode because ID text is 999
    which is not in array.

    Thanks
  • Martin Honnen

    #2
    Re: Elegant way to remove all nodes from XML whose child has textthat IS NOT in array

    jsmith wrote:
    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>.Wher e(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").Inn erText)) Then
    ins.ParentNode. RemoveChild(ins )
    End If
    Next

    doc.Save(Consol e.Out)

    that should work with .NET 1.0 and later.





    --

    Martin Honnen --- MVP XML

    Comment

    Working...