如何从层次结构列表(对象)中删除记录 [英] How to delete a record from a hierarchy list(of object)
本文介绍了如何从层次结构列表(对象)中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将我的XML选择为IEnumerable对象,然后将其转换为List(of Object)。现在,我想在匹配的Id上从列表中搜索和删除一些记录。怎么做?
我的XML文件示例
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 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>
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
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:
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
推荐答案
你不能删除For Each循环中列表中的项目 - 它会改变你循环的列表,并且总是会抛出异常,而且非常正确。
将For Each改为For,并从最后到开头向后工作,你可以将它们删除。
或者,设置一个新的List并添加你要删除的每个项目然后使用第二个For Each循环来调用List。删除每个人。
没有任何线索可以做到正确,我在这里寻求帮助。
将For Each改为For,然后从最后到开始向后工作,你可以将它们删除。
嗯,你知道如何将For Each改为For:
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:
Dim myList As New List(Of String)()
For Each s As String In myList
Console.WriteLine(s)
Next
成为:
Becomes:
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
是吗?
所以写下For,它从最高指数运行到最低:
Yes?
So write the For so it runs from the highest index to the lowest:
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:
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
有意义吗?
这篇关于如何从层次结构列表(对象)中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文