2013-07-24 30 views
-1

第一個列表「AStore」迭代併爲每次迭代創建一個新列表。第二個List「APages」迭代,但不會在每次迭代中創建一個新列表。每個列表創建和List.Add具有相同的位置。這裏有什麼問題?C#嵌套列表不添加新列表

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 
    } 

回答

1

我對每個列表創建

有創作的同一位置時,AStore在for循環外創建的同一位置,且APages一個是。