1

在 C# 中对数据进行分组,我已经解析了 html 文件并获取了其中的所有数据,现在我想将它们分组如下:

在此处输入图像描述

选择的那些行是父行并包含以下子代,我正在处理的代码在这里:

var uricontent = File.ReadAllText("TestHtml/Bew.html");
            var doc = new HtmlDocument(); // with HTML Agility pack
            doc.LoadHtml(uricontent);

            var rooms = doc.DocumentNode.SelectNodes("//table[@class='rates']").SelectMany(
                detail =>
                {

                    return doc.DocumentNode.SelectNodes("//td[@class='rate-description'] | //table[@class='rooms']//h2 | //table[@class='rooms']//td[@class='room-price room-price-total']").Select(
                        r => new
                        {
                            RoomType = r.InnerText.CleanInnerText(),
                        });
                }).ToArray();

RoomType 包含由 HTML AgilityPack 解析的数据,我如何按名称对它们进行分组,例如 Pay & Save ,仅限最佳可用房间...

HTML 文件在这里: http: //notepad.cc/share/g0zh0TcyaG

谢谢

4

1 回答 1

0

与其合并 3 个 XPath 查询,然后尝试通过“速率描述”(也称为元素 : <td class="rate-description">)将它们分组,您可以用另一种方式进行。

您可以通过“房价描述”来选择 LINQ,然后在投影部分,使用相对 XPath 获取当前“房价描述”下的所有房间类型和房价:

var rooms = 
    doc.DocumentNode
       .SelectNodes("//table[@class='rates']//tr[@class='rate']")
       .Select(r => new
         {
            RateType = r.SelectSingleNode("./td[@class='rate-description']")
                        .InnerText.CleanInnerText,
            RoomTypes = r.SelectNodes("./following-sibling::tr[@class='rooms'][1]//table[@class='rooms']//h2")
                         .Select(s => new
                         {
                            RoomType = s.InnerText.CleanInnerText,
                            Rate = s.SelectSingleNode(".//parent::td/following-sibling::td[@class='room-price room-price-total'][1]")
                                    .InnerText.CleanInnerText
                         }).ToArray()
         }).ToArray();

上面一些 XPath 查询开始时的通知期。这HtmlAgilityPack表明查询是相对于 current 的HtmlNode。结果大约是这样的:

在此处输入图像描述

于 2014-04-12T12:50:22.640 回答