2010년 7월 7일 수요일

HTMLAgilityPack solution

  1. Download and build the HTMLAgilityPack solution.

  2. In your application, add a reference to HTMLAgilityPack.dll in the HTMLAgilityPack\Debug (or Realease) \bin folder.

Then, as an example:


                
            HtmlAgilityPack
            .
            HtmlDocument
             htmlDoc 
            =
             
            new
             
            HtmlAgilityPack
            .
            HtmlDocument
            ();
            
              

// There are various options, set as needed
htmlDoc
. OptionFixNestedTags = true ;

// filePath is a path to a file containing the html
htmlDoc
. Load ( filePath );

// Use: htmlDoc.LoadXML(xmlString); to load from a string

// ParseErrors is an ArrayList containing any errors from the Load statement
if ( htmlDoc . ParseErrors != null && htmlDoc . ParseErrors . Count > 0 )
{
// Handle any parse errors as required

}
else
{

if ( htmlDoc . DocumentNode != null )
{
HtmlNode bodyNode = htmlDoc . DocumentNode . SelectSingleNode ( "//body" );

if ( bodyNode != null )
{
// Do something with bodyNode
}
}
}

(NB: This code is an example only and not necessarily the best/only approach. Do not use it blindly in your own application.)

The HtmlDocument.Load() method also accepts a stream which is very useful in integrating with other stream oriented classes in the .NET framework. While HtmlEntity.DeEntitize() is another useful method for processing html entities correctly. (thanks Matthew)

HtmlDocument and HtmlNode are the classes you'll use most. Similar to an XML parser, it provides the selectSingleNode and selectNodes methods that accept XPath expressions.

Pay attention to the HtmlDocument.Option?????? boolean properties. These control how the Load and LoadXML methods will process your HTML/XHTML.

There is also a compiled help file called HtmlAgilityPack.chm that has a complete reference for each of the objects. This is normally in the base folder of the solution.

5
Also note that Load accepts a Stream parameter, which is convenient in many situations. I used it for a HTTP stream (WebResponse.GetResponseStream). Another good method to be aware of is HtmlEntity.DeEntitize (part of HTML Agility Pack). This is needed to process entities manually in some cases. - Matthew Flaschen May 11 '09 at 7:34
note: in the latest beta of Html Agility Pack (1.4.0 Beta 2 released Oct 3 2009) the help file has been moved out into a separate download because of dependencies on Sandcastle, DocProject and the Visual Studio 2008 SDK. - rtpHarry Apr 6 at 23:02

댓글 없음:

댓글 쓰기