C # parsing HTML documents

Source: Internet
Author: User

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:



Related Article

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.