简体   繁体   English

如何使用HtmlAgility Pack解析WPF中div id标记之间的值?

[英]How to parse the values between div id tag in WPF using HtmlAgility Pack?

I have a html file like this: 我有一个这样的html文件:

<div id="id_1">
Some Texts 
</div>
<div id ="id_2">
Some Texts
</div>

How can I get all the texts between each div id tag? 如何获取每个div id标签之间的所有文本? My question is related to WPF. 我的问题与WPF有关。

Here is my code: 这是我的代码:

private void button_click(object sender, RoutedEventArgs e) {
    HtmlDocument doc = new HtmlDocument();
    doc.Load("file.html");
    HtmlNode nodes = doc.DocumentNode.SelectNodes("//div[@id='id_1']");
    var text = nodes.InnerText;
    MessageBox.Show(text);
}       

I am scraping this source: 我正在抓取此来源:

<div id="id_1">
   Some Texts 1
</div>
<div id ="id_2">
   Some Texts 2
</div>

using: 使用:

HtmlDocument doc = new HtmlDocument();
doc.Load("C:\\temp\\stackhtml.html");
int i = 1;
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div");
foreach(HtmlNode node in nodes)
{
    string text = node.InnerText; 
    Console.WriteLine("text" + i.ToString() + ": " + text);
    i++;
}
Console.ReadLine();

result: 结果:

在此处输入图片说明

You can do with you want with the strings, populate and array, etc.... 您可以对字符串,填充和数组等进行处理。

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

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