3

我想解析以下 HTML。

我目前拥有的是

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

html是

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

我需要从标签中获取数字li data和标签之间的值span。任何帮助表示赞赏。

4

1 回答 1

6

像这样的东西可能适合您的需求。

//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);
}
于 2012-08-22T23:38:48.370 回答