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); } } }
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.
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:
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.
You should use LoadHtml
method with loads a string. Load
method loads from file
htmlDoc.LoadHtml(hotel.InnerText);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With