1

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>

各強力なノードの右側にあるテキストを取得するにはどうすればよいですか?

たとえば、コース ID を取得するには、「Course321」という文字列になりました。

コード:

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;
        }
    }
}

現在のコード:

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;
}
4

2 に答える 2

1

following-sibling::* XPath 軸の使用:

HtmlNode sibling = strong.SelectSingleNode("following-sibling::text()");
Console.WriteLine("Course ID = " + sibling.InnerText.Trim());
于 2012-08-02T11:48:04.460 に答える
0

私の XPathofobia を共有している方は、これにより、兄弟投稿の強力なタグを取得できます。

new HtmlDocument().LoadHtml("blah blah blah").DocumentNode.DescendantsAndSelf().Where (dn => dn.Name == "strong").Select (dn => dn.NextSibling.InnerText)
于 2013-03-24T17:17:26.023 に答える