Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I select my XML into IEnumerable object and then convert it into List(of Object). Now, I want to search and delete some record from the list base on matched Id. How to do that?

Example of my XML file
XML
<?xml version="1.0" encoding="utf-8" ?>
<groups defaultIcon="resources/images/modules-icons/icon.png">
  <group title="Customer">
    <item>
      <title>Customer File</title>
      <item-icon>resources/images/modules-icons/CustomerSupplierFile48x48.png</item-icon>
      <id>customers</id>
    </item>

    <item>
      <title>Customer Group</title>
      <item-icon>resources/images/modules-icons/sink.png</item-icon>
      <id>customers</id>
    </item>

    <item>
      <title>Shipping Address</title>
      <id>customershippingaddress</id>
    </item>


  </group>
  
  <group title="Supplier">
    <item>
      <title>Suppliers Setup</title>
      <item-icon>resources/images/modules-icons/CustomerSupplierFile48x48.png</item-icon>
      <id>suppliers</id>
    </item>

    <item>
      <title>Supplier</title>
      <id>supplier</id>
    </item>

  </group>
  
  <group title="Other group">
    <item>
      <title>Inventory</title>
        <id>inventory</id>
    </item>

    <item>
      <title>Suppliers</title>
      <id>suppliers</id>
    </item>

    <item>
      <title>Help</title>
      <id>help</id>
    </item>

    <item>
      <title>Options</title>
      <id>options</id>
    </item>

    <item>
      <title>Employees</title>
      <id>employees</id>
    </item>
  </group>
</groups>


C#
Dim document As XElement = XElement.Load(Server.MapPath("resources/MasterFiles.xml"))
         Dim defaultIcon As String = If(document.Attribute("defaultIcon") IsNot Nothing, document.Attribute("defaultIcon").Value, "")

         Dim query As IEnumerable(Of Object) = From g In document.Elements("group")
                                               Select New With {
                                                    .Title = If(g.Attribute("title") IsNot Nothing, g.Attribute("title").Value, ""),
                                                    .Items = (From i In g.Elements("item")
                                                              Select New With {

                                                             .Title = 
If(i.Element("title") IsNot Nothing, i.Element("title").Value, ""),
                                                             .Icon = If(i.Element("item-icon") IsNot Nothing, i.Element("item-icon").Value, defaultIcon),
                                                             .Id = If(i.Element("id") IsNot Nothing, i.Element("id").Value, ""),
                                                             .Visible = "Y"
                                                       })
                                            }

         ''Then convert it into List() for easy manage
         ''
         Dim qlist As List(Of Object) = query.ToList()


Here is what I try but do know how to delete the matched record

VB
   For x = qlist.Count - 1 To 0 Step -1
                'Loop tru And get user module access
                For Each itm In qlist(x).Items
                    If itm.Id = "customershippingaddress" Then
'I can find it but how to do next?

                    End If
                Next
            Next


What I have tried:

VB
   For x = qlist.Count - 1 To 0 Step -1
                'Loop tru And get user module access
                For Each itm In qlist(x).Items
                    If itm.Id = "customershippingaddress" Then
'I can find it but how to do next?

                    End If
                Next
            Next
Posted
Updated 22-Sep-16 22:09pm

1 solution

You can't delete items in a list inside a For Each loop - it changes the list you are loop ingon, and that will always throw an exception, and very rightly.
Change your For Each to a For, and work backwards from the end to the beginning and you can delete them fine.
Alternatively, set up a new List and add each item you want to remove to that and then use a second For Each loop to call List.Remove for each of them.

" have no clue to get it right and I am seeking for help here.
"Change your For Each to a For, and work backwards from the end to the beginning and you can delete them fine.""


Well, you know how to change a For Each to a For:
VB.NET
Dim myList As New List(Of String)()
For Each s As String In myList
    Console.WriteLine(s)
Next
Becomes:
VB
Dim myList As New List(Of String)()
For i As Integer = 0 To myList.Length - 1
	Dim s As String = myList(i)
	Console.WriteLine(s)
Next
Yes?
So write the For so it runs from the highest index to the lowest:
VB
Dim myList As New List(Of String)()
For i As Integer = myList.Length - 1 To 0 Step -1
	Dim s As String = myList(i)
	Console.WriteLine(s)
Next
That way, when you delete an item it doesn't alter the index of the next item you will look at:
VB
Dim myList As New List(Of String)()
For i As Integer = myList.Length - 1 To 0 Step -1
	Dim s As String = myList(i)
	If s.StartsWith("Delete me") Then
		myList.Remove(s)
	End If
Next
Make sense?
 
Share this answer
 
v2
Comments
CoderWil 23-Sep-16 4:20am    
Thanks for your suggestion. I had tried several methods including "For" method. However, I couldn't get the sub elements. Will appreciate if you can give me a head up of your suggestion.
OriginalGriff 23-Sep-16 4:33am    
You can't convert For Each loops into For loops yourself?
You are kidding, right?
CoderWil 23-Sep-16 4:41am    
I have no clue to get it right and I am seeking for help here.
"Change your For Each to a For, and work backwards from the end to the beginning and you can delete them fine."
OriginalGriff 23-Sep-16 4:55am    
Answer updated
CoderWil 24-Sep-16 4:40am    
Thank you OriginalGriff.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900