4
protected Dictionary<string , string> xmlList = new Dictionary<string , string>();
protected System.Collections.ArrayList list = new System.Collections.ArrayList();

我已将字典存储在这样的数组列表中..

    xmlList.Add( "image" , "images/piece1.png" );
    xmlList.Add( "description" , " Experience why entertainment is more amazing with Xbox."            );
    xmlList.Add( "title" , "Downloads" );
    list.Add( xmlList );
    xmlList.Clear();
    xmlList.Add( "image" , "images/piece2.png" );
    xmlList.Add( "description" , "Limited-time offer: Buy Office now, get the next version free.*" );
    xmlList.Add( "title" , "Security & Updates" );
    list.Add( xmlList );

如何从 arraylist 访问字典的每个元素?

<% for (int i = 0; i < list.Count; i++)
    {
    foreach(Dictionary<string , string> itemList in list)
        {
        Response.Write( itemList["image"] );
        }
   } 
%>

这给了我两次相同的结果'images/piece2.png'..

我做不到

foreach(Dictionary<string , string> itemList in list[i])
    {
    Response.Write( itemList["image"] );
    }
4

4 回答 4

2
 protected Dictionary<string, string> xmlList;
    protected System.Collections.ArrayList list = new System.Collections.ArrayList();

 xmlList = new Dictionary<string, string>();
        xmlList.Add("image", "images/piece1.png");
        xmlList.Add("description", " Experience why entertainment is more amazing with Xbox.");
        xmlList.Add("title", "Downloads");
        list.Add(xmlList);
        xmlList = new Dictionary<string, string>();
        xmlList.Add("image", "images/piece2.png");
        xmlList.Add("description", "Limited-time offer: Buy Office now, get the next version free.*");
        xmlList.Add("title", "Security & Updates");
        list.Add(xmlList);

        foreach (Dictionary<string, string> itemList in list)
        {
            Response.Write(itemList["image"]);
            Response.Write("<br>");
        }
于 2012-12-07T05:28:34.970 回答
1

1)使用通用List<T>而不是ArrayList

 Dictionary<string, string> xmlList = new Dictionary<string, string>();
 List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();

2)如果你想在列表中有两个单独的字典,你需要创建其中两个,否则你有两个对同一个字典的引用。所以:

 list.Add(xmlList);
 xmlList = new Dictionary<string, string>(); //instead of xmlList.Clear();
 //...
 list.Add(xmlList);

3) 现在您可以执行以下操作来遍历字典列表:

 foreach (Dictionary<string, string> d in list)
 {
     //...
 }
于 2012-12-07T06:19:07.787 回答
0

我感觉您两次使用相同的对象 xmllist。这就是您两次获得图像的原因。请尝试 xmllist1 和 xmllist2。遍历字典和数组列表的所有其他答案对我来说都是正确的。这是可以的原因。所以请试试这个。

 xmlList1.Add( "image" , "images/piece1.png" );
    xmlList1.Add( "description" , " Experience why entertainment is more amazing with Xbox."            );
    xmlList1.Add( "title" , "Downloads" );
    list.Add( xmlList1 );

    xmlList2.Add( "image" , "images/piece2.png" );
    xmlList2.Add( "description" , "Limited-time offer: Buy Office now, get the next version free.*" );
    xmlList2.Add( "title" , "Security & Updates" );
    list.Add( xmlList2 );

然后使用 CodeIgnoto 的方法。

于 2012-12-07T06:29:25.773 回答
0

您可以使用此代码段遍历列表中的每个字典并获取字典中的每个元素。

foreach(Dictionary<string,string> xdict in list)
{
    foreach(var xkey in xdict.Keys)
    {
       Response.Write(xdict[xkey]);
    }
}
于 2012-12-07T05:28:43.263 回答