Получите все значения атрибутов данного тега с помощью Html Agility Pack

StackOverflow https://stackoverflow.com/questions/2462552

Вопрос

Я хочу получить все значения атрибута 'id' тега 'span' с помощью html agility pack.Но вместо атрибутов я получил сами теги.Вот код

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

Я буду признателен, если кто-нибудь скажет мне, что здесь не так.

Это было полезно?

Решение

попробуй

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;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top