Gagan Sikri

Gagan Sikri

  • NA
  • 1.6k
  • 217.4k

Parsing data to ASP.NET from HTML using HtmlAgilityPack

Jul 24 2015 8:41 AM
I am using HTMLAgilityPack to get data from a html page according to user request and then I want to parse data from that HTML page to my ASP.NET page and then export it to JSON. Please help me out how to do that.
Code for ASP.NET Source Page is:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="httprequest_web.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<hr />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
&nbsp;<asp:TextBox ID="TextBox1" runat="server" MaxLength="6" TextMode="Number"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
&nbsp;<asp:Label ID="Label3" runat="server" Text="Label" Visible="False"></asp:Label>
<hr />
<br />
<asp:Label ID="Label4" runat="server" Text="Label" Visible="False"></asp:Label>
<br />
</div>
</form>
</body>
</html>
CS Code for respective ASP.NET Page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using HtmlAgilityPack;
namespace httprequest_web
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Label1.Text = "Train Running Status/ JSON Output";
Label2.Text = "Please enter Train No.:";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Generation of HTTP request from the train number specified in text box.
try
{
Label3.Visible = false;
//get request
string base_url = "http://railenquiry.in/runningstatus/";
string url_request = string.Concat(base_url, TextBox1.Text);
//using HtmlAgilityPack
WebClient rq = new WebClient();
string getdata = rq.DownloadString(url_request);
HtmlAgilityPack.HtmlDocument rec_data = new HtmlAgilityPack.HtmlDocument();
rec_data.LoadHtml(getdata);
List<List<string>> table = rec_data.DocumentNode.SelectSingleNode("//table[@class='table table-striped table-vcenter table-bordered table-responsive']")
.Descendants("tr").Where(tr => tr.Elements("td").Count() >= 1).Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList()).ToList();
}
catch
{
Label3.Visible = true;
Label3.Text = "Something went wrong. Please try again!";
}
}
}
}
 
 
Please tell me how can I take output on my ASP.NET page as well as export it into JSON in well structured form so that I can use it later on using Newtonsoft JSON(Json.net)?