使用HtmlAgilityPack解析多个值 [英] Parse multiple values using HtmlAgilityPack

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

问题描述

大家好,



我将再次发布此问题,因为在处理html文件时,使用xml的响应将无效。我刚刚开始搞乱HAP,我在弄清楚如何获得一些价值观方面遇到了一些困难。



我使用这个文件作为例子,我是将返回的值存储到listview中,问题是我不知道如何获取该部分的每个值。



Hi Everyone,

I am going to post this question again as the response for using xml will not work when dealing with html files. I just started messing with HAP and I am having some difficulties in figuring out how to get some of my values.

I am using this file as an example and I am storing the returned values into a listview, problem is I don't know how to go about in getting each value on the section.

<bookstore>
<book>
   <title lang="en">Harry Potter</title>
   <price>29.99</price>
   <available>In Stock</available>
</book>

<book>
   <title lang="en">Learning XML</title>
   <price>39.95</price>
   <available>In Stock</available>
</book>

<book>
   <title lang="en">Learning C#</title>
   <price>59.95</price>
   <available>Backorder</available>
</book>

<book>
   <title lang="en">Learning Java</title>
   <price>39.95</price>
   <available>In Stock</available>
</book>
</bookstore>



有人能告诉我一个关于如何遍历树并每次获得每本书的每个值的示例吗?



这就是我现在所知道的。




Can someone show me an example on how to traverse the tree and getting each value for each of the books one at a time?

This is all I know how to do right now.

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load("sample.txt");

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//title"))
{
    ListViewItem lView = new ListViewItem();
    lView.Text = node.InnerText;
    lView.SubItems.Add("");
    lView.SubItems.Add("");
    listView1.Items.Add(lView);
}





感谢任何帮助。



Appreciate any help.

推荐答案

你好theadmin,



从我理解的问题来看,你的要求是获得书店下每本书的标题,价格和可用性。下面的代码确实如此。



Hi theadmin,

From the question I understand your requirement is to get the title,price and available for each book under bookstore. Code below does exactly that.

string input =
                "<bookstore><book><title>Harry Potter</title><price>29.99</price><available>In Stock</available></book><book>" +
                "<title>Learning XML</title><price>39.95</price><available>In Stock</available></book><book><title>Learning C#</title>" +
                "<price>59.95</price><available>Backorder</available></book><book><title>Learning Java</title><price>39.95</price>" +
                "<available>In Stock</available></book></bookstore>"

            HtmlDocument html = new HtmlDocument();
            html.LoadHtml(input);

            HtmlNodeCollection bookStore = html.DocumentNode.SelectNodes("//bookstore");
            HtmlNodeCollection books = bookStore[0].SelectNodes("//book");
            foreach (HtmlNode book in books)
            {
                var bookDetail = from child in book.ChildNodes
                    select child.InnerText;
            }





快乐编码。:)



Happy coding.:)


嗨theadmin,


网页可能很复杂。但我假设您有兴趣获取< bookstore><下的所有< book>< / book> 节点的详细信息; / bookstore> 来自复杂网页的节点。只需对我已经共享的代码进行少量自定义,您就可以实现所需。这是你如何做到的(假设你有Listview)。



Hi theadmin,

The webpage can be complex. But I am assuming, you are interested in getting the details of all the <book></book> nodes under <bookstore></bookstore> node from the complex web page. You can achieve what you need with little customization of the code I already shared. Here is how you can do it (assuming you have the Listview).

HtmlNodeCollection bookStore = html.DocumentNode.SelectNodes("//bookstore");
HtmlNodeCollection books = bookStore[0].SelectNodes("//book");
foreach (HtmlNode book in books)
{
    Listview.Text = book.ChildNodes["title"].InnerText;
    Listview.Subitems.Add(book.ChildNodes["price"].InnerText);
    Listview.Subitems.Add(book.ChildNodes["available"].InnerText);
}



希望这有帮助。


Hope this helps.


这篇关于使用HtmlAgilityPack解析多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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