HtmlAgilityPack : illegal characters in path

Pranab picture Pranab · Feb 21, 2014 · Viewed 8.1k times · Source

I'm getting an "illegal characters in path" error in this code. I've mentioned "Error Occuring Here" as a comment in the line where the error is occuring.

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);
        }
    }
}

Answer

L.B picture L.B · Feb 21, 2014

You should use LoadHtml method with loads a string. Load method loads from file

htmlDoc.LoadHtml(hotel.InnerText);