Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlAgilityPack : illegal characters in path

Tags:

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);         }     } } 
like image 230
Pranab Avatar asked Feb 21 '14 07:02

Pranab


People also ask

Are there any illegal characters in a string with the path?

These are not needed and are illegal characters in a path. How are you initializing the string with the path? This can be seen from the debugger visualizer, as the string starts with "" and ends with "", it shows that the quotes are part of the string, when they shouldn't be.

Is it possible to escape a string in a path?

You can do two thing - a regular escaped string (using \) or a verbatim string literal (that starts with a @ ): The string is surrounded by double quotes. Yes, that's not a valid character in a path. You should probably tackle it at the source, but you can strip them out with:

How to check if a path contains an invalid character?

Use System.IO.Path.CheckInvalidPathChars () to check which characters are invalid and maybe contained in the argument provided to the method above. Sorry the path is just the domain root we haven't changed anything on the site for a long while so this is out of the blue.


1 Answers

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

htmlDoc.LoadHtml(hotel.InnerText);    
like image 101
L.B Avatar answered Sep 19 '22 20:09

L.B