Navigating MSHTML from C# without a WebBrowser control

2023-10-16 23:38

本文主要是介绍Navigating MSHTML from C# without a WebBrowser control,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 http://www.experts-exchange.com/Web_Development/Components/ActiveX/Q_24168307.html

http://radio.javaranch.com/balajidl/2006/01/18/1137606354980.html

http://msdn.microsoft.com/en-us/library/Aa290341

 

 方法一:

VB.NET的语法,可以改成C#,里面的createDocumentFromUrl略有误,改为IHTMLDocument4即可

可惜不能Create from string

 

Make sure you add reference to Microsoft.mshtml from the .NET objects collection and "Imports System.Runtime.InteropServices"

 

 

'We will use HTMLDocument to open and load remote webpage in to IHTMLDocument2
'we can't use the same HTMLDocument as it is needed for persistance(IPersistStream)
'we also can't use IHTMLDocument2 object as it will not have DOM interface faetures enabled. we will use IHTMLDocument3. 
Dim url as String = http://java.sun.com 
Dim objMSHTML As New mshtml.HTMLDocument
Dim objMSHTML2 As mshtml.IHTMLDocument2
Dim objMSHTML3 As mshtml.IHTMLDocument3
Dim x As Integer = 10 'a dummy variable
Dim objIPS As IPersistStreamInit 'here is the whole trick
objIPS = DirectCast(objMSHTML, IPersistStreamInit)
objIPS.InitNew() 'you have to do it, if not you will always have readyState as "loading"
objMSHTML2 = objMSHTML.createDocumentFromUrl(url, vbNullString)
Do Until objMSHTML2.readyState = "complete"
x = x + 1
Application.DoEvents 'Suggested by John
Loop
objMSHTML3 = DirectCast(objMSHTML2, mshtml.IHTMLDocument3)




 

Now you can start using DOM interfaces like getElementByID(), getElementsByTagName(..) etc.,
-------------------------------------------------------------------------------------------------------------------------------------------------------------
方法二:
原来IHTMLDOCUMENT2是可以直接写入string的
http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c
//Another alternative would be to use the builtin engine mshtml:
using mshtml; 
... 
object[] oPageText = { html }; 
HTMLDocument doc = new HTMLDocumentClass(); 
IHTMLDocument2 doc2 = (IHTMLDocument2)doc; 
doc2.write(oPageText); 
//This allows you to use javascript-like functions like getElementById()

-------------------------------------------------------------------------------------------------------------------------------------
方法3:
这个fizzler分析器,是基于HTMLAgilityPack的HTML代码分析库,用法与Jquery相同,使用起来相当方便.
相比早前使用的Winista.HtmlParser,优胜了很多,推荐使用.
http://htmlagilitypack.codeplex.com/SourceControl/list/changesets
 HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
doc.Save("file.htm");

http://code.google.com/p/fizzler/
// Load the document using HTMLAgilityPack as normal 
var html = new HtmlDocument(); 
html.LoadHtml(@"   <html>       <head></head>       <body>         <div>           <p class='content'>Fizzler</p>           <p>CSS Selector Engine</p></div>       </body>   </html>");  
// Fizzler for HtmlAgilityPack is implemented as the  
// QuerySelectorAll extension method on HtmlNode  
var document = htmlDocument.DocumentNode;  
// yields: [<p class="content">Fizzler</p>] 
document.QuerySelectorAll(".content");   
// yields: [<p class="content">Fizzler</p>,<p>CSS Selector Engine</p>] document.QuerySelectorAll("p");  
// yields empty sequence 
document.QuerySelectorAll("body>p"); 
// yields [<p class="content">Fizzler</p>,<p>CSS Selector Engine</p>] document.QuerySelectorAll("body p");  
// yields [<p class="content">Fizzler</p>] 
document.QuerySelectorAll("p:first-child");

这篇关于Navigating MSHTML from C# without a WebBrowser control的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/221514

相关文章

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n