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.