1
HTML:使用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 protected]<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;
}