How to use HTML Agility pack

后端 未结 7 1806
闹比i
闹比i 2020-11-21 04:30

How do I use the HTML Agility Pack?

My XHTML document is not completely valid. That\'s why I wanted to use it. How do I use it in my project? My project is in C#.

7条回答
  •  悲哀的现实
    2020-11-21 05:24

    First, install the HTMLAgilityPack nuget package into your project.

    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.LoadHtml(xmlString);  to load from a string (was htmlDoc.LoadXML(xmlString)
    
    // 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)
        {
            HtmlAgilityPack.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.

提交回复
热议问题