C # Use markup service to parse HTML into DOM tree

Source: Internet
Author: User

A lightweight parsing implementation. ThisCodeIt does not download any materials from the Internet, nor execute any scripts. It is purely parsing.

Parsing is implemented through the markup service of mshtml. To use this code correctly, you need to add mshtml reference.
Because the ipersiststreamint interface is not defined in. net, you must implement it yourself. The interface definition is as follows:

The following content:ProgramCode:

[Comvisible (true), comimport (), GUID ("7fd52380-4e07-101b-ae2d-08002b2ec713"), interfacetypeattribute (cominterfacetype. interfaceisiunknown)]
Public interface ipersiststreaminit
{
Void getclassid ([In, out] ref guid pclassid );
[Return: financialas (unmanagedtype. I4)] [preservesig]
Int isdirty ();
Void load ([IN, financialas (unmanagedtype. Interface)] ucomistream PSTM );
Void save ([IN, financialas (unmanagedtype. Interface)] ucomistream PSTM,
[IN, financialas (unmanagedtype. I4)] int fcleardirty );
Void getsizemax ([out, financialas (unmanagedtype. lparray)] Long queue size );
Void initnew ();
}

The following is the program code:

Unsafe ihtmldocument2 parse (string S)
{
Ihtmldocument2 pdocument = new htmldocumentclass ();
If (pdocument! = NULL)
{
Ipersiststreaminit ppersist = pdocument as ipersiststreaminit;
Ppersist. initnew ();
Ppersist = NULL;
Imarkupservices MS = pdocument as imarkupservices;
If (MS! = NULL)
{
Imarkupcontainer PMC = NULL;
Imarkuppointer pstart, pend;
Ms. createmarkuppointer (Out pstart );
Ms. createmarkuppointer (Out pend );
Stringbuilder sb = new stringbuilder (s );
Intptr psource = marshal. stringtohglobaluni (s );
Ms. parsestring (ref * (ushort *) psource. topointer (), 0, out PMC, pstart, pend );
If (PMC! = NULL)
{
Marshal. Release (psource );
Return PMC as ihtmldocument2;
}
Marshal. Release (psource );
}
}
Return NULL;
}

A problem occurred when writing the code. The first parameter of imarkupservice: parsestring is ref ushort. It is clear that the HTML code must be passed in. This ushort must be the first widechar, so here we use Insecure code to bypass the compiler warning.

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.