从Webbrowser控件获取页面标题 [英] Get Title of the page from webbrowser control

查看:115
本文介绍了从Webbrowser控件获取页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C#的新手,我正在尝试对服务器执行一些Web服务请求.

我已经使用Web浏览器从服务器加载页面.我需要阅读页面标题并在我的应用程序中对菜单进行一些更改.

我在Google上搜索,找到了找到的解决方案,例如尝试使用HTMLDocument,webbrowser1.DocumentText等.但是它们都不起作用.

我需要这里的专家帮助,如何解决这个问题?

我想到的一些解决方案是:

1.以某种方式下载文件并将其存储在temp目录中,并使用本地URL加载Web浏览器.
==>可以使用执行此操作的任何API吗?

2.是否可以使用xmlhttprequest方法获取信息?
==>如果是这样怎么办?在C#中使用此方法的任何示例?

任何类型的信息,链接,示例等都将有很大帮助.

预先感谢,
问候,
Vinay

Hi,

I am very new to C#, I am trying to do some web service request to my server.

I have used web browser to load a page from the server. I need to read the Title of the page and make some changes to the menu in my app.

I googled and found found solutions like try using HTMLDocument, webbrowser1.DocumentText etc. But none of them worked.

I request help from the experts here, how I can over come this problem?

Some solutions which came to my mind are:

1. Download the file using some way and store it in a temp directory and load the web browser with the local URL.
==> Any API''s for doing this is available?

2. is there a possibility of using xmlhttprequest methods to get the info?
==> if so how? any sample of using this method in C#?

Any kind of information, links, samples etc., would be of great help.

Thanks in advance,
Regards,
Vinay

推荐答案

Web浏览器控件具有一个名为Document的属性,其类型为HtmlDocument.通过MSDN文档,可以找到一种名为GetElementsByName的方法: http://msdn .microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx [
The web browser control has a property called Document and it''s type is HtmlDocument. Going bythe MSDN documentation there is a method called GetElementsByName : http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx[^].
So when you call it like this:

HtmlDocument doc = webbrowser1.Document;
HtmlElementCollection elems = doc.GetElementsByTagName("TITLE");
String title = String.Empty;
if(elems.Count > 0)
{
    HtmlElement elem = elems[0];
    title = elem.InnerText;
}



上面的代码在您的HTML文档中找到标题标签并获取其内容.

干杯!



The above code finds the title tag in you HTML document and fetches it''s content.

Cheers!


虽然这并不是您问题的真正答案",但它可能会一直为您提供帮助.
由于在Net 2.0 Compact Framework Web浏览器控件中,没有标题属性,并且您无法访问文档对象,因此可能需要以一些不同的方式来执行此操作.查看
帖子,尤其是指向其中的"Webby".我对该软件不熟悉,但它可能至少会给您带来另一个思考过程.
Although this isn''t really an "answer" to your question, it may help you along the way.
Since in the net 2.0 compact framework web browser control, there is no title property and you can''t access the document object you will perhaps need to do this in a little different way. Check out this post, specifically the link to "Webby" in it. I''m not familiar with that piece of software, but it may at least give you another thought process.


这篇关于从Webbrowser控件获取页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆