C#HtmlAgilityPack解析< ul> [英] C# HtmlAgilityPack parse <ul>

查看:446
本文介绍了C#HtmlAgilityPack解析< ul>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分析以下HTML。

I want to parse the following HTML.

我现在拥有的是

What I currently have is

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

html是

The html is

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

我需要从 li data span 标记之间的值。任何帮助表示赞赏。

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

推荐答案

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

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

这篇关于C#HtmlAgilityPack解析&lt; ul&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆