Reputation: 3177
I'm a student working with htmlagilitypack for the 1st time. I'm currently filtering the html to get the values using
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;
}
As you can see, I'm checking if the current node contains a value "NAME" and if so i want to get the next node's value next to it. I will be very thankful if you can help me out with this.
The Value of the name is in the next node <div class='content-b'>THIS IS MY NAME</div>
.. how can i problematically tell C# to get the next node before looping again ?
Thank you.
Update : Here is the html code fragment that im working with, It Looks ugly , I'm sorry I can't help it
<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>
Thank you.
Upvotes: 1
Views: 7175
Reputation: 18443
Firstly, there is no class="content-b"
in your document. They are all spelled as contet-b
.
Secondly, since the html is not well-formed, NextSibling
will not work. You need to manually get the next node matching the criteria. You can't use a foreach
loop in this situation.
Here is the result:
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
}
}
}
Upvotes: 2
Reputation: 634
What you're looking for is NextSibling property of node. In your example:
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;
}
Upvotes: 1