通过LINQ获取属性值

通过LINQ获取属性值,linq,html-agility-pack,getelementbyid,Linq,Html Agility Pack,Getelementbyid,HTML源代码如下所示 <img id="itemImage" src="https://www.xyz.com/item1.jpg"> 但是imageURL提供了如下输出: System.Linq.Enumerable+WhereSelectEnumerableIterator`2[HtmlAgilityPack.HtmlNode,System.String] 问题是把它抛到绳子上Select()返回IEnumerable,因此基本上将枚举数转换为字符串(如错误消息所示)。调用

HTML源代码如下所示

<img id="itemImage" src="https://www.xyz.com/item1.jpg">
但是imageURL提供了如下输出:

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

问题是把它抛到绳子上
Select()
返回
IEnumerable
,因此基本上将枚举数转换为字符串(如错误消息所示)。调用
First()
Single()
Take(1)
,以便在将单个元素转换为字符串之前获取该元素

.Select(node => node.Attributes["src"].Value).First().ToString();
另外,如果所需元素可能不存在,
FirstOrDefault()
SingleOrDefault()
返回null,而不是抛出异常。那样的话,我建议

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

问题是把它抛到绳子上
Select()
返回
IEnumerable
,因此基本上将枚举数转换为字符串(如错误消息所示)。调用
First()
Single()
Take(1)
,以便在将单个元素转换为字符串之前获取该元素

.Select(node => node.Attributes["src"].Value).First().ToString();
另外,如果所需元素可能不存在,
FirstOrDefault()
SingleOrDefault()
返回null,而不是抛出异常。那样的话,我建议

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

问题是把它抛到绳子上
Select()
返回
IEnumerable
,因此基本上将枚举数转换为字符串(如错误消息所示)。调用
First()
Single()
Take(1)
,以便在将单个元素转换为字符串之前获取该元素

.Select(node => node.Attributes["src"].Value).First().ToString();
另外,如果所需元素可能不存在,
FirstOrDefault()
SingleOrDefault()
返回null,而不是抛出异常。那样的话,我建议

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

问题是把它抛到绳子上
Select()
返回
IEnumerable
,因此基本上将枚举数转换为字符串(如错误消息所示)。调用
First()
Single()
Take(1)
,以便在将单个元素转换为字符串之前获取该元素

.Select(node => node.Attributes["src"].Value).First().ToString();
另外,如果所需元素可能不存在,
FirstOrDefault()
SingleOrDefault()
返回null,而不是抛出异常。那样的话,我建议

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

尝试添加
FirstOrDefault()


尝试添加
FirstOrDefault()


尝试添加
FirstOrDefault()


尝试添加
FirstOrDefault()

Add.DefaultIfEmpty(string.Empty) .第一道防线

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();
Add.DefaultIfEmpty(string.Empty) .第一道防线

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();
Add.DefaultIfEmpty(string.Empty) .第一道防线

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();
Add.DefaultIfEmpty(string.Empty) .第一道防线

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