views:

532

answers:

1

Hi,

I'm still playing around with xml. Now I have a file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Attributes>
 <AttributeSet id="10110">
  <Attribute id="1">some text here</Attribute>
  <Attribute id="2">some text here</Attribute>
  <!-- 298 more Attribute nodes follow -->
  <!-- note that the value for the id attribute is numbered consecutively -->
 </AttributeSet>
</Attributes>

There are 300 Attribute nodes total, most of which I don't need. What I'd like to do is remove all Attribute nodes that don't have a specified value for the id attribute. I have established a string array with about 10 values. These values represent the attributes that I'd like to keep in the xml. The rest I'd like to remove.

What I'm trying to do with the code below is modify the xml by removing all Attribute nodes that I don't want to use:

Dim ss() As String = New String() {"39", "41", "38", "111", "148", "222", "256", "270", "283", "284"} 'keep the Attributes whose id value is one of these numbers
Dim rv As New List(Of String)'will hold Attribute ids to remove
Dim bool As Boolean = False
For Each x As XElement In doc...<eb:Attribute>
 For Each s As String In ss
  If x.@id = s Then
   bool = True
   Exit For
  End If
 Next
 If bool = True Then
  'do nothing
 Else 'no attribute matched any of the attribute ids listed mark xelement for removal
  rv.Add(x.@id)
 End If
Next
'now remove the xelement
For Each tr As String In rv
 Dim h As String = tr
 doc...<eb:Attribute>.Where(Function(g) g.@id = h).Remove()
Next
'save the xml
doc.Save("C:\myXMLFile.xml")

For some reason, my code doesn't work. None of the undesired Attribute nodes are removed.

What's the problem? How can I remove the Attribute nodes whose id attribute values don't match any number in my string array?

Thanks in advance.

P.S. - I hope I made myself clear in describing my problem.

A: 

Never mind. I figured it out. Here what I did:

For Each x As XElement In doc...<eb:Attribute>
 **bool = False 'I simply added this line of code and everything worked perfectly**
 For Each s As String In ss
  If x.@id = s Then
   bool = True
   Exit For
  End If
 Next
 If bool = True Then
  'do nothing
 Else 'no attribute matched any of the attribute ids listed so remove the xelement
  rv.Add(x.@id)
 End If
Next
Dude-Dastic