harishannam
harishannam

Reputation: 2796

Get Attribute Value by LINQ

The HTML Source is as follows

<img id="itemImage" src="https://www.xyz.com/item1.jpg">

I am using the following LINQ query to get the SRC value (Image Link)

string imageURL = document.DocumentNode.Descendants("img")
                    .Where(node => node.Attributes["id"] != null && node.Attributes["id"].Value == "itemImage")
                    .Select(node => node.Attributes["src"].Value).ToString();

But the imageURL gives output as

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[HtmlAgilityPack.HtmlNode,System.String]

Upvotes: 0

Views: 1689

Answers (3)

neodim
neodim

Reputation: 477

Add .DefaultIfEmpty(string.Empty) .FirstOrDefault

string imageURL = document.DocumentNode.Descendants("img")
                .Where(node => node.Attributes["id"] != null && node.Attributes["id"].Value == "itemImage")
                .Select(node => node.Attributes["src"].Value)
                .DefaultIfEmpty(string.Empty)
                .FirstOrDefault()
                .ToString();

Upvotes: 1

w.b
w.b

Reputation: 11228

Try adding FirstOrDefault():

string imageURL = document.DocumentNode.Descendants("img")
                .Where(node => node.Attributes["id"] != null && node.Attributes["id"].Value == "itemImage")
                .Select(node => node.Attributes["src"].Value)
                .FirstOrDefault();

Upvotes: 0

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

The problem is casting it to string. Select() returns IEnumerable<T> so you are basically converting an enumerator to a string (as the error message says). Call First() or Single() or Take(1) in order to get a single element before casting it to a string.

.Select(node => node.Attributes["src"].Value).First().ToString();

Also, if there is a chance that the desired element is not present, FirstOrDefault() and SingleOrDefault() returns null rather then throwing an exception. In that case, I would recommend

var imageUlr = ... .Select(node => node.Attributes["src"].Value).FirstOrDefault();
if (imageUrl != null)
{
    // cast it to string and do something with it
}

Upvotes: 2

Related Questions