【问题标题】:Get all attribute values of given tag with Html Agility Pack使用 Html Agility Pack 获取给定标签的所有属性值
【发布时间】:2011-01-28 14:03:43
【问题描述】:

我想用 html 敏捷包获取 'span' 标签的 'id' 属性的所有值。 但是我没有属性,而是自己获得了标签。这是代码

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

如果有人告诉我这里出了什么问题,我将不胜感激。

【问题讨论】:

    标签: c# html xml xpath html-agility-pack


    【解决方案1】:

    试试

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      相关资源
      最近更新 更多