选择下一个节点html敏捷包c#

本文关键字:html 下一个 节点 选择 | 更新日期: 2023-09-27 18:04:16

我是第一次使用htmlililitpack的学生。我目前正在过滤html,以获得使用

的值
foreach (HtmlNode link in bodyNode.SelectNodes("//span[@class='content-b']"))
{
    if (link.InnerText.Contains("Name"))
    {
        //MessageBox.Show("Found");
        textBox1.Text += "Name : " + ?????;
    }
    textBox1.Text += link.InnerText;
}

正如你所看到的,我正在检查当前节点是否包含一个值"NAME",如果是,我想获得下一个节点旁边的值。如果你能帮我解决这个问题,我将非常感激。

名称的值在下一个节点<div class='content-b'>THIS IS MY NAME</div> ..我怎么能告诉c#在再次循环之前得到下一个节点?

谢谢。


更新:这是我工作的html代码片段,它看起来很丑,我很抱歉我不能帮助它

<span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Name of the Author: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">Undertaker</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Name movie: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">Some Movie Name</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Room Online: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">skype123</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Subsites and site: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">google.nl</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Year: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">2013. </font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Genre: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">Horror</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Length: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">00:35:45 </font></font></span></span></span></span><span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Description: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class=""><font>Paragraph 1</font><font>Paragraph 2</font><font>Paragraph 3</font></font><br></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font>Video Format: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font>MP4 </font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font>Video: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font>MPEG4 Video (H264) 720x404 29.97fps 1000Kbps </font></font></span></span></span></span><span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font>Audio: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font>AAC 44100Hz stereo 96Kbps</font></font></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

谢谢。

选择下一个节点html敏捷包c#

首先,您的文档中没有class="content-b"。它们都拼写为 contet-b

其次,由于html不是格式良好的,NextSibling将不起作用。您需要手动获取与条件匹配的下一个节点。在这种情况下,不能使用foreach循环。

结果如下:

var nodes = bodyNode.SelectNodes("//div[@class='contet-b']").ToList();
for( int i =0; i < nodes.Count; i++)
{
    var link = nodes[i];
    if (link.InnerText.Contains("Name"))
    {
        textBox1.Text += "Name : ";
        if (i + 1 < nodes.Count)
        {
            // append the value of next matching `div` node
            textBox1.Text += nodes[i + 1].InnerText.Trim();
            i++; // skip this node
        }
    }
}

你要找的是节点的NextSibling属性。在您的示例中:

foreach (HtmlNode link in bodyNode.SelectNodes("//div[@class='content-b']"))
{
    if (link.InnerText.Contains("Name"))
    {
        //MessageBox.Show("Found");
        textBox1.Text += "Name : " + link.NextSibling.InnerText;
    }
    textBox1.Text += link.InnerText;
}