2013-04-28 105 views
0

HTML的幫助事業部之間的字符串是:掌握類名

<div class="topheader-left common-txt" style="color: #ffffff;vertical-align:top;padding-top:3px"> 
      &nbsp;Sunday,&nbsp; April&nbsp;&nbsp;28,&nbsp;2013&nbsp;|&nbsp;08:07:07 PM 
     </div> 

我需要的時間是8點07分07秒,我將轉換爲時間價值。 我正在使用此代碼獲取Div區域的字符串。獲取子串。爲什麼這不起作用?

HtmlElementCollection theElementCollection = default(HtmlElementCollection); 
       theElementCollection = webBrowser1.Document.GetElementsByTagName("div"); 
       foreach (HtmlElement curElement in theElementCollection) 
       { 
        if (curElement.GetAttribute("class").ToString() == "topheader-left common-txt") 
        { 
         MessageBox.Show(curElement.GetAttribute("InnerText")); 

        } 
       } 
+2

我期望這個使用目的內置HTML解析器如[HTML敏捷性包(http://htmlagilitypack.codeplex.com/)(的XPath),或要容易得多[CsQuery ](https://github.com/jamietre/CsQuery)(jQuery選擇器語法) – Oded 2013-04-28 16:27:34

+0

已配置HTML Agility Pack。現在代碼是什麼? – Braheen 2013-04-28 16:38:57

回答

0

HTML敏捷性包配置。現在代碼是什麼?

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
doc.LoadHtml(html); 

var innerText = doc.DocumentNode 
        .SelectSingleNode("//div[@class='topheader-left common-txt']") 
        .InnerText; 


DateTime time = DateTime.ParseExact(WebUtility.HtmlDecode(innerText).Split('|')[1].Trim(), 
            "hh:mm:ss tt", 
            CultureInfo.InvariantCulture);