C# HtmlAgilityPack parse <ul>

ぃ、小莉子 提交于 2019-12-06 07:24:07

问题


I want to parse the following HTML.

What I currently have is

var node = document.DocumentNode.SelectSingleNode("//div[@class='wrapper']");

The html is

<div class="wrapper">
    <ul>
                <li data="334040566050326217">
                    <span>test1</span>
                </li>
                <li data="334040566050326447">
                    <span>test2</span>
                </li>
    </ul>

I need to get the number from the li data and the value between the span tag. Any help appreciated.


回答1:


Something like this might suit your needs.

//Assumes your document is loaded into a variable named 'document'

List<string> dataAttribute = new List<string>(); //This will contain the long # in the data attribute
List<string> spanText = new List<string>();      //This will contain the text between the <span> tags
HtmlNodeCollection nodeCollection = document.DocumentNode.SelectNodes("//div[@class='wrapper']//li");

foreach (HtmlNode node in nodeCollection)
{
    dataAttribute.Add(node.GetAttributeValue("data", "null"));
    spanText.Add(node.SelectSingleNode("span").InnerText);
}


来源:https://stackoverflow.com/questions/12082281/c-sharp-htmlagilitypack-parse-ul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!