2016-04-27 48 views
0

請原諒我的英語,它不是我的第一語言。C#列表和HtmlParse

第1類是我有我的財產和列表 第2類是我的html解析器。

目前,它的工作是打開網頁並找到鏈接。

現在我遇到的問題是,我想使用Linq,另一個List,或者你認爲最好的。 進行排序studRet(填充NameID)的結果

因此,所有從 「itsupporter」 的 「href發現結果」 是由
Itsupporter
結果
結果

finans
結果排序
結果

我該怎麼做,最好的做法是什麼,有什麼好的例子可以指導我解答?

我不知道如何確保,它找到的鏈接是從右側studRet。

我希望這是一個足夠好的問題。

Class1的

private readonly List<Property> dataList = new List<Property>(); 
    // Get property 
    internal struct Property 
    { 
     public readonly string url; 
     public readonly string cssTag; 
     public readonly string studRet; 
     public readonly int id; 

     public Property(string url, string cssTag, string studRet, int id) 
     { 

      this.url = url; 
      this.cssTag = cssTag; 
      this.studRet = studRet; 
      this.id = id; 

     } 
    } 

    public List<Property> GetList() 
    { 
     dataList.Add(new Property("http://elevplads.dk/annoncer?search=It-Supporter", "//div[@class='col-xs-12 col-sm-12 col-md-12']//a", "itsupporter", 1)); 
     dataList.Add(new Property("http://elevplads.dk/annoncer?stillingstype=finans", "//div[@class='col-xs-12 col-sm-12 col-md-12']//a", "finans", 2)); 
     return dataList; 
    } 

2級

public readonly Jobbot Jobdata = new Jobbot(); 
public void HandleHtml() 
    { 
     List<Jobbot.Property> dataList = Jobdata.GetList();   

     HtmlWeb web = new HtmlWeb(); 

     foreach (var data in dataList) 
     {    
      HtmlDocument document = web.Load(data.url); 
      var nodes = document.DocumentNode.SelectNodes(data.cssTag); 
      if (nodes != null) 
      { 
       foreach (HtmlNode item in nodes) 
       { 
        if (item.Attributes.Contains("href")) 
        { 
         if (item.Attributes["href"].Value.StartsWith("http://")) 
         { 

          Console.WriteLine(item.Attributes["href"].Value); 
         } 
        } 
       } 
      } 
     } 
    } // End 

回答

0

我不太確定我明白你的問題。您已經按照您希望結果集的順序發送Web請求,所以我不明白爲什麼您需要對它們進行排序。至於映射與studRet屬性的鏈接,你可以把結果放到一個列表:

public void HandleHtml() 

...  

List<Tuple<string, Jobbot.Property>> results = new List<Tuple<string, Jobbot.Property>>(); 

... 

          if (item.Attributes["href"].Value.StartsWith("http://")) 
          { 
           results.Add(Tuple.Create(item.Attributes["href"].Value, data)); 
           Console.WriteLine(item.Attributes["href"].Value); 
          } 

,然後如果你真的需要對結果進行排序,你可以使用:

results.OrderBy(x => x.Item2.studRet); 

但是如前所述之前,在你的情況下,這是沒有必要的。

+0

我看着它,你說得對。感謝使它實現它。 – user1979911