2014-02-21 95 views
17

我在此代碼中收到「路徑中的非法字符」錯誤。我提到「錯誤發生在這裏」作爲錯誤發生的行中的註釋。HtmlAgilityPack:路徑中的非法字符

var document = htmlWeb.Load(searchUrl); 
var hotels = document.DocumentNode.Descendants("div") 
      .Where(x => x.Attributes.Contains("class") && 
      x.Attributes["class"].Value.Contains("listing-content")); 

int count = 1; 
foreach (var hotel in hotels) 
{ 
    HtmlDocument htmlDoc = new HtmlDocument(); 
    htmlDoc.OptionFixNestedTags = true; 
    htmlDoc.Load(hotel.InnerText);  // Error Occuring Here // 
    if (htmlDoc.DocumentNode != null) 
    { 
     var hotelName = htmlDoc.DocumentNode.SelectNodes("//div[@class='business-container-inner']//div[@class='business-content clearfix']//div[@class='business-name-wrapper']//h3[@class='business-name fn org']//div[@class='srp-business-name']//a[0]"); 
     foreach (var name in hotelName) 
     { 
      Console.WriteLine(name.InnerHtml); 
     } 
    } 
} 

回答

39

您應該使用LoadHtml方法加載字符串。從Load方法加載文件

htmlDoc.LoadHtml(hotel.InnerText); 
+1

Thak你LB.這工作。我沒有注意到那個小小的錯誤。感謝您的時間和迴應。 – Pranab

4

這只是意味着你正在嘗試加載與invalid character in the file path/name的文件。

的錯誤是在這裏:

htmlDoc.Load(hotel.InnerText); 

..because是超負荷預計文件路徑:

public void Load(string path) 

使用LoadHtml加載HTML片段:

public void LoadHtml(string html) 
相關問題