C#读取html标签中的数据

C#读取html标签中的数据

问题描述:

如何读取html标签(如<strong>)之间的数据。例如。C#读取html标签中的数据

<strong id="food">40</strong> 

如何从那里检索整数40?我试图

myBrowser.Document.GetElementById("food").GetAttribute("value");

+0

也许40是食品的则firstChild? – user1096188 2012-02-29 18:24:18

+0

http://*.com/questions/56107/what-is-the-best-way-to-parse-html-in-c 这就是我在我的应用程序中使用。 – 2012-02-29 18:25:05

+0

你确定这是C#吗?看起来更像JavaScript。 – Paddy 2012-02-29 18:25:20

您正在寻找的InnerHtml财产。

+0

现在,首先我印象深刻的是,我得到了多快的回复(认为至少应该是一天)。其次,谢谢大家的好回答! – cranzy 2012-02-29 18:42:30

我相信你正在寻找InnerText。

+0

或者在这里,InnerHTML会做到这一点,因为SLaks在我之前发布。 – 2012-02-29 18:26:59

的innerText

[STAThread] 
static void Main(string[] args) 
{ 
    WebBrowser w = new WebBrowser(); 
    w.Navigate(String.Empty); 
    HtmlDocument doc = w.Document; 
    doc.Write("<html><head></head><body><p id=\"food\">40</p></body></html>"); 
    Console.WriteLine(doc.GetElementById("food").InnerText); 
    Console.ReadKey(); 
}