【问题标题】:fetch dictionary from arraylist从数组列表中获取字典
【发布时间】:2012-11-25 07:03:15
【问题描述】:
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 );

如何从数组列表中访问字典的每个元素?

<% 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"] );
    }

【问题讨论】:

    标签: c# asp.net dictionary arraylist


    【解决方案1】:
     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>");
            }
    

    【讨论】:

    • 它给了我以下错误消息 foreach 语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义
    【解决方案2】:

    您可以使用此 sn-p 遍历列表中的每个字典并获取字典中的每个元素。

    foreach(Dictionary<string,string> xdict in list)
    {
        foreach(var xkey in xdict.Keys)
        {
           Response.Write(xdict[xkey]);
        }
    }
    

    【讨论】:

    • 感谢您的回复,但这也显示了两次相同的结果。像这样 images/piece2.png 限时优惠:立即购买 Office,免费获得下一个版本。*安全和更新images/ piece2.png 限时优惠:立即购买 Office,免费获得下一个版本。*安全和更新
    • 不确定你所说的两次是什么意思。我认为你添加字典两次,否则这很好
    【解决方案3】:

    1) 使用通用List&lt;T&gt; 而不是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)
     {
         //...
     }
    

    【讨论】:

      【解决方案4】:

      我感觉您两次使用相同的对象 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的方法。

      【讨论】:

        猜你喜欢
        • 2023-01-24
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 2016-01-10
        • 1970-01-01
        相关资源
        最近更新 更多