【问题标题】:C# Nested List not adding a new listC#嵌套列表不添加新列表
【发布时间】:2013-07-24 03:55:11
【问题描述】:

第一个列表AStore 迭代并为每次迭代创建一个新列表。第二个 List APages 进行迭代,但不会在每次迭代时创建新列表。对于每个列表创建和 List.Add,我都有相同的位置。这里有什么问题?

public void Promos()
{
    //get store info and id
    var storeinfo = new HtmlWeb();
    var storeshtm = storeinfo.Load(@"Stores.htm");
    var nodes = storeshtm.DocumentNode.SelectNodes("div");
    List<Store> AStore = new List<Store>();
    nodes = nodes[0].ChildNodes;
    int a = 0;
    foreach (var node in nodes)
    {
        if (node.Name != "#text")
        {
            AStore.Add(new Store(
                node.ChildNodes[1].ChildNodes[1].Attributes[7].Value,//storewebid
                "Astore",//storename
                node.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[3].InnerText,//storeaddress
                node.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[5].InnerText,//storecity
                node.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[5].InnerText,//storestate
                node.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[5].InnerText,//storezip
                node.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[7].InnerText,//storephone
                ""//storehours
            ));
        }
    }
    for (int i = 0; i <= a; i++)
    {
        var circualr = new HtmlWeb();
        var storehtm = circualr.Load(@"http://storewebsite/" + AStore[i].StoreWebID);
        var cnodes = storehtm.DocumentNode.SelectNodes("//*[@id="+'"'+"Wrapper"+'"'+"]");
        List<Pages> APages = new List<Pages>();
        foreach (var cnode in cnodes)
            if(cnode.ChildNodes[3].ChildNodes[3].ChildNodes[5].ChildNodes[3].ChildNodes[1].Name == "a")
                APages.Add(new Pages(cnode.ChildNodes[3].ChildNodes[3].ChildNodes[5].ChildNodes[3].ChildNodes[1].Attributes[2].Value));//get inner page links
    }

【问题讨论】:

    标签: c# list arraylist nested


    【解决方案1】:

    每次创建列表时我都有相同的位置

    您确实具有相同的创建位置,AStore 是在 for 循环之外创建的,APages 是。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多