Use com to call ie for HTML Parsing

Source: Internet
Author: User
Let's not talk about anything else. Code , The code is very simple, do not understand the message. Code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using System. runtime. interopservices;
6 Using Mshtml;
7 Using System. Threading;
8 Using System. runtime. interopservices. comtypes;
9 Using System. IO;
10
11 Namespace Eric. Utilities. html
12 {
13 Public   Enum Hresult
14 {
15 E_fail =   - 2147467259 ,
16 E_invalidarg =   - 2147024809 ,
17 E_nointerface =   - 2147467262 ,
18 E_notimpl =   - 2147467263 ,
19 E_unexpected =   - 2147418113 ,
20 S_false =   1 ,
21 S_ OK =   0
22 }
23
24 [Comimport, GUID ( " 0000010c-0000-0000-c000-000000000046 " ), Interfacetype (cominterfacetype. interfaceisiunknown), comvisible ( True )]
25 Public   Interface Ipersist
26 {
27VoidGetclassid (RefGuid pclassid );
28}
29
30 [Comimport, interfacetype (cominterfacetype. interfaceisiunknown), comvisible ( True ), GUID ( " 7fd52380-4e07-101b-ae2d-08002b2ec713 " )]
31 Public   Interface Ipersiststreaminit: ipersist
32 {
33 New   Void Getclassid ( Ref Guid pclassid );
34 [Preservesig]
35 Int Isdirty ();
36 [Preservesig]
37 Hresult load (istream PSTM );
38 [Preservesig]
39 Hresult save (istream PSTM, [financialas (unmanagedtype. bool)] Bool Fcleardirty );
40 [Preservesig]
41 Hresult getsizemax ([In, out, financialas (unmanagedtype. u8)] Ref   Long Pcbsize );
42 [Preservesig]
43 Hresult initnew ();
44 }
45
46 Public   Class Htmlparser
47 {
48 Public Ihtmldocument3 parse ( String URL)
49 {
50 Htmldocument objmshtml =   New Htmldocument ();
51 Ihtmldocument2 objmshtml2;
52 Ihtmldocument3 objmshtml3;
53
54 Ipersiststreaminit objips;
55 Objips = Objmshtml As Ipersiststreaminit;
56 Objips. initnew ();
57 Objips =   Null ;
58
59 Objmshtml2 = Objmshtml. createdocumentfromurl (URL, " Null " );
60 While (Objmshtml2.readystate ! =   " Complete " )
61 {
62Thread. Sleep (1000);
63}
64 Objmshtml3 = Objmshtml2 As Ihtmldocument3;
65 Return Objmshtml3;
66 }
67
68 Public Ihtmldocument3 parsehtml ( String HTML, encoding)
69 {
70 String Tmpfile = Path. gettempfilename ();
71 File. writealltext (tmpfile, HTML, encoding );
72 Return Parse (tmpfile );
73 }
74 }
75 }
76

To solve this problem, we first created a temporary file for parsehtml. If we wanted to create a temporary file, we could not do it directly, but we had to use the makup service on the Internet, but I found it unavailable. I wonder if you have any good solutions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.