Data Calling from WEB
into
C# Without any Service or Extended Library
By
Syed Ubaid Ali Jafri
Dated: 19th Feb, 2015
Introduction
This Article describes the concept of Receiving a Data from a Web into C#.Net. Many Websites contain Data
that is being updated on Real time basis for example (Stock Exchange, Weather Forecasting, Flight
Reservations, Hotel Reservations, Cinema Ticket Booking, Different Countries Time Zone).
Many Programmers somehow requires that data to be integrated with their application.
Example:
We are considering a Time Zone from http://www.worldtimeserver.com
Task is to retrieve the Current Date of Pakistan .
Requirement
2 Labels and 1 Button, 1 Web Browser is Required
Here
If we do a quick look in the source code of the original website it looks
string className = "font6";
string globalPattern;
globalPattern = String.Format("<span [^>]*class=("|')font6("|')>(.*?)</span>",className);
using "font6" as a span class. we can further enhance this code by calling Font7 which is the time class in the
source code.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.worldtimeserver.com/current_time_in_PK.aspx");
}
private void button1_Click(object sender, EventArgs e)
{
WebClient web = new WebClient();
string url = string.Format("http://www.worldtimeserver.com/current_time_in_PK.aspx" );
string response = web.DownloadString(url);
string className = "font6";
string globalPattern;
globalPattern = String.Format("<span [^>]*class=("|')font6("|')>(.*?)</span>",className);
//<div[^>]*?class=(["'])[^>]*{0}[^>]*1[^>]*>(.*?)</div>", className);
Regex regx = new Regex(globalPattern, RegexOptions.Singleline | RegexOptions.Compiled |
RegexOptions.IgnoreCase);
Match match = regx.Match(response);
label1.Text = match.ToString();
string html = "<span class = 'font7'></span><span class="font7"></span>";
string newregex = "<span [^>]*class=("|')font7("|')>(.*?)</span>";
MatchCollection matches = new Regex(newregex, RegexOptions.Multiline |
RegexOptions.Compiled).Matches(html);
Match matcch = regx.Match(response);
string contents = matcch.Groups[3].Value;
label2.Text = contents;
}

Data calling from web to C#

  • 1.
    Data Calling fromWEB into C# Without any Service or Extended Library By Syed Ubaid Ali Jafri
  • 2.
  • 3.
    Introduction This Article describesthe concept of Receiving a Data from a Web into C#.Net. Many Websites contain Data that is being updated on Real time basis for example (Stock Exchange, Weather Forecasting, Flight Reservations, Hotel Reservations, Cinema Ticket Booking, Different Countries Time Zone). Many Programmers somehow requires that data to be integrated with their application. Example: We are considering a Time Zone from http://www.worldtimeserver.com Task is to retrieve the Current Date of Pakistan . Requirement 2 Labels and 1 Button, 1 Web Browser is Required
  • 4.
    Here If we doa quick look in the source code of the original website it looks string className = "font6"; string globalPattern; globalPattern = String.Format("<span [^>]*class=("|')font6("|')>(.*?)</span>",className); using "font6" as a span class. we can further enhance this code by calling Font7 which is the time class in the source code.
  • 5.
    private void Form1_Load(objectsender, EventArgs e) { webBrowser1.Navigate("http://www.worldtimeserver.com/current_time_in_PK.aspx"); } private void button1_Click(object sender, EventArgs e) { WebClient web = new WebClient(); string url = string.Format("http://www.worldtimeserver.com/current_time_in_PK.aspx" ); string response = web.DownloadString(url); string className = "font6"; string globalPattern; globalPattern = String.Format("<span [^>]*class=("|')font6("|')>(.*?)</span>",className); //<div[^>]*?class=(["'])[^>]*{0}[^>]*1[^>]*>(.*?)</div>", className); Regex regx = new Regex(globalPattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); Match match = regx.Match(response); label1.Text = match.ToString(); string html = "<span class = 'font7'></span><span class="font7"></span>"; string newregex = "<span [^>]*class=("|')font7("|')>(.*?)</span>"; MatchCollection matches = new Regex(newregex, RegexOptions.Multiline | RegexOptions.Compiled).Matches(html); Match matcch = regx.Match(response); string contents = matcch.Groups[3].Value; label2.Text = contents; }