Htmlの敏捷性パックで与えられたタグのすべての属性値を取得します。
-
20-09-2019 - |
質問
私は、HTML敏捷性パックと「スパン」タグの「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);
}
誰かがここで間違っているものを私に伝えた場合、私は感謝します。
解決
してみてください。
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;
所属していません StackOverflow