HTML parsing component HtmlAgilityPack

Source: Internet
Author: User

HtmlAgilityPack is an open-source class library for parsing HTML elements. The biggest feature is that you can use XPath to parse HMTL. If you have used C # To operate XML, you can also use HtmlAgilityPack. The latest version is 1.4.6, as shown below:

Http://htmlagilitypack.codeplex.com/

The following is a simple example to describe the use of HtmlAgilityPack. to simulate logon, except for the name attribute values in the User name text box and Password text box, you also need to know the values of the hidden controls _ VIEWSTATE and _ EVENTVALIDATION on the page, as well as the name attribute of the submit button. The following describes how to use HtmlAgilityPack to obtain this additional value.

1. add reference to HtmlAgilityPack. dll to the project.

2. put several text box controls and a button control on the Aspx page.

3. The button background events are as follows:

  btnHtml_Click( sender, EventArgs e)        {             (tbUrl.Text.Length > 0)            {                HtmlWeb htmlWeb =  HtmlWeb();                HtmlDocument htmlDoc = htmlWeb.Load(.tbUrl.Text);                HtmlNode htmlNode = htmlDoc.DocumentNode.SelectSingleNode("");                 viewStateValue = htmlNode.Attributes[""].Value;                htmlNode = htmlDoc.DocumentNode.SelectSingleNode("");                 eventValidation = htmlNode.Attributes[""].Value;                htmlNode = htmlDoc.DocumentNode.SelectSingleNode("");                 submitName = htmlNode.Attributes[""].Value;                tbViewState.Text = viewStateValue;                tbEventValidation.Text = eventValidation;                tbSubmitName.Text = submitName;            }        }

4. Take the logon interface of the blog garden as an example. The obtained interface is as follows:

Sample download

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.