2012-08-02 46 views
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; 
} 

回答

1

使用以下同胞:: * XPath的轴:

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

对于那些你们谁分享我的XPathofobia,这会做t o获取兄弟姐妹帖子强标签:

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