I believe many people have the need to parse HTML documents. For example, if we capture the page data of a website, the format is HTML. In the past, we used regular expressions for parsing, but we found some problems. It is not easy to parse HTML documents. If the document format is slightly changed, it is likely that the matching is not correct. Therefore, we need dedicated tools to help us easily parse HTML documents.
In fact, there is already a very good tool. For example, HtmlAgilityPack. It can help us parse HTML documents as easily and conveniently as using the XmlDocument class to parse XML.
This tool can be downloaded at http://htmlagilitypack.codeplex.com/and contains dll that supports various .net Framework versions.
Okay. Here is an example of Simple enough. On this basis, you can draw a line from the other.
For example, parse the following HTML.
Time |
Type |
Name |
Unit |
Amount |
|
Invoice 1 |
Procurement material invoice 1 |
Company 1 |
$123 |
|
Invoice 2 |
Procurement material invoice 2 |
XX Company 2 |
$321 |
Take the console project as an example. You must first reference the HtmlAgilityPack. dll file to use the classes and methods in the dll.
Static void Main (string [] args) {string strWebContent = @"
Time |
Type |
Name |
Unit |
Amount |
"+ @"
|
Invoice 1 |
Procurement material invoice 1 |
Company 1 |
$123 |
"+ @"
|
Invoice 2 |
Procurement material invoice 2 |
XX Company 2 |
$321 |
"; ListDatas = new List(); // Define a list to save the result HtmlDocument htmlDocument = new HtmlDocument (); htmlDocument. loadHtml (strWebContent); // load the HTML string. If it is a file, you can use htmlDocument. load Method to Load HtmlNodeCollection collection = htmlDocument. documentNode. selectSingleNode ("table/tbody "). childNodes; // similar to Xpath, you can easily locate the foreach (HtmlNode node in collection) under the corresponding node {// remove \ r \ n and spaces, get the data in the corresponding td string [] line = node. innerText. split (new char [] {'\ R',' \ n', ''}, StringSplitOptions. removeEmptyEntries); // if the conditions are met, load it to the object list if (line. length = 5) datas. add (new Data () {time = line [0], type = line [1], name = line [2], unit = line [3], amount = line [4]});} // check whether the result is correct in a loop (var v in datas) {Console. writeLine (string. join (",", v. time, v. type, v. name, v. unit: v. amount ));}}The above is the complete code, and the comments are also clear.
Finally, let's take a look at the parsing result: