I am using HTML Agility to get all the images since image dont always have absolute path i am trying to do following. But line marked below in the code generates error
Cannot implicitly convert type ‘System.Uri’ to ‘System.Collections.Generic.List’
I am not sure how to fix this i tried so many option but keep on getting one or the other error
List<String> imgList = (from x in doc.DocumentNode.Descendants("img")
where x.Attributes["src"] != null
select x.Attributes["src"].Value.ToLower()).ToList<String>();
List<String> AbsoluteImageUrl = new List<String>();
foreach (String element in imgList)
{
AbsoluteImageUrl = new Uri(baseUrl, element); //GIVES ERROR
}
The compiler generates an error, because the type of
AbsoluteImageUrlis not compatible with the type of yourUri. If you need to add theUrito a string list, you should obtain its underlying string (e.g.Uri.AbsolutePath). In this case, the code would look like this:On the other hand, if you need an
Urilist instead, keep your original code and change the type ofAbsoluteImageUrl:Once this is done, you should use
AbsoluteImageUrl.Addin the loop to add theUrito the list.Regarding the discussion in comments about difference between
Uri.ToString()andUri.AbsolutePath, they have a different definitions per the official MSDN, so it depends on the OP’s requirements which he/she should use. The source code ofUri.ToString, on a side note, is as follows, so it is fundamentally different fromAbsolutePath: