選擇下一個節點html Agility pack C#

c# filtering html-agility-pack regex

我是第一次使用htmlagilitypack的學生。我目前正在過濾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>

謝謝。

一般承認的答案

首先,您的文檔中沒有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;
}


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因