Using HTML Agility Pack to grab text to the right of a node

c# html html-agility-pack

Question

HTML:

<strong>Capture Date/Time:</strong> August 1, 2012 1:05:00 PM EST<br>
<strong>Instructor:</strong> Ash<br>
<strong>Instructor Email:</strong> email@email.com<br>
<strong>Course ID:</strong> Course321<br>

How would I go about getting the text on the right of each strong node?

For example, to get the course ID so I ended up with a string of "Course321".

Code:

private string getCourseID()
{
    foreach (HtmlAgilityPack.HtmlNode strong in htmlDoc.DocumentNode.SelectNodes("//strong"))
    {
        string innerText = strong.InnerText;

        if (innerText.Contains("Course ID"))
        {
            //select the outer text
            //return outertext;
        }
    }
}

Current Code:

private string getCourseID()
{
    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

    string value = "Error";

    foreach (HtmlAgilityPack.HtmlNode strong in htmlDoc.DocumentNode.SelectNodes("//strong"))
    {
        string innerText = strong.InnerText;

        if (innerText.Contains("Course ID"))
        {
            HtmlAgilityPack.HtmlNode sibling = strong.SelectSingleNode("following-sibling::text()");

            value = sibling.InnerText.Trim();

            MessageBox.Show(value);
        }
    }

    return value;
}

Accepted Answer

Using the following-sibling::* XPath axis:

HtmlNode sibling = strong.SelectSingleNode("following-sibling::text()");
Console.WriteLine("Course ID = " + sibling.InnerText.Trim());

Popular Answer

For those of you who share my XPathofobia, this would do to obtain the sibling(s) post strong-tags :

new HtmlDocument().LoadHtml("blah blah blah").DocumentNode.DescendantsAndSelf().Where (dn => dn.Name == "strong").Select (dn => dn.NextSibling.InnerText)



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why