问题描述
我需要解析html文件中的选择值.我有这个html文件:
I need parse a select value in html file. I have this html file:
<html>
<head></head>
<body>
<select id="region" name="region">
<option value="0" selected>Všetky regiony</option>
<optgroup>Banskobystrický kraj</optgroup>
<option value="k_1">Banskobystrický kraj</option>
<option value="1">Banská Bystrica</option>
<option value="3">Banská Štiavnica</option>
<option value="18">Brezno</option>
<option value="22">Detva</option>
<option value="58">Dudince</option>
</select>
</body>
</html>
我需要获取选择选项值以及字典中的文本值.我将此文件加载到webBrowser组件中,尝试通过ID"region"获取选择标签.
I need get select option value and also text value in dictionary. I load this file in webBrowser component a try get select tag by ID "region".
webBrowser1.Url = new Uri("file://\\C:\\1.html");
if (webBrowser1.Document != null)
{
HtmlElement elems = webBrowser1.Document.GetElementById("region");
}
但是对象elems为null,这不是为什么.有进步吗?
But object elems is null, I don’t why. Any advance?
Html Agillity Pack已解决问题.谢谢大家我很傻,我宁愿先听Html Agillity Pack的建议.
Problem was resolved with Html Agillity Pack. Thank for everybody. I was stupid, I had rather listen to your advice with Html Agillity Pack first.
推荐答案
您可以使用 HtmlAgilityPack HtmlAgilityPack .有许多使用它来解析html的示例.您可以通过Google搜索找到.这里有一些:
You can do it with the HtmlAgilityPack. There are many examples of using it to parsing html. You can find via a google search. Here are a few:
http://htmlagilitypack.codeplex.com/wikipage?title=Examples& ; referringTitle = Home
更新:
虽然我认为使用库是一个更好的选择,但是您可以通过以下方式使用webbrowser控件来做到这一点:
While I think using the library is a better choice, you can do it with the webbrowser control in the following manner:
webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(ParseOptions);
webBrowser1.Url = new Uri("C:\\1.html", UriKind.Absolute);
private void ParseOptions(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement elems = webBrowser1.Document.GetElementById("region");
}
请注意,解析是在 DocumentCompleted 事件处理程序.
Notice that the parsing is done in the DocumentCompleted event handler.
这篇关于解析C#中的HTML combox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!