繁体   English   中英

C#HtmlAgilityPack解析<ul>

[英]C# HtmlAgilityPack parse <ul>

我想解析以下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 dataspan标记之间的值中获取数字。 任何帮助赞赏。

这样的事情可能适合您的需求。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM