2013-04-28 104 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);