获取给定标签的所有属性值的HTML敏捷性包 [英] Get all attribute values of given tag with Html Agility Pack

查看:214
本文介绍了获取给定标签的所有属性值的HTML敏捷性包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到的HTML敏捷包跨度标签的id属性的所有值。
,而不是属性我的标签自理。下面的代码

I want to get all values of 'id' attribute of 'span' tag with html agility pack. But instead of attributes I got tags themself. Here's the code

        private static IEnumerable<string> GetAllID()
        {
            HtmlDocument sourceDocument = new HtmlDocument();
            sourceDocument.Load(FileName);
            var nodes = sourceDocument.DocumentNode.SelectNodes(
                 @"//span/@id");
            return nodes.Nodes().Select(x => x.Name);
        }

如果有人告诉我什么是错在这里,我会感激。

I'll appreciate if someone tells me what's wrong here.

推荐答案

尝试

var nodes = sourceDocument.DocumentNode.SelectNodes("//span[@id]");
List<string> ids = new List<string>(nodes.Count);

if(nodes != null)
{
    foreach(var node in nodes)
    {
        if(node.Id != null)
        ids.Add(node.Id);
    }
}

return ids;

这篇关于获取给定标签的所有属性值的HTML敏捷性包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆