This Forum has been archived there is no more new posts or threads ... use this link to report any abusive content
==> Report abusive content in this page <==
Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying a c# outcome in a web page (html)?
10-01-2012, 12:07 PM
Post: #1
Displaying a c# outcome in a web page (html)?
I've created a piece of code that imports a text file containing a list of hashtags from twitter and outputs it as a data dictionary showing how many there is of each hashtag, showing only top 20 etc.

So far, I can get this to display in the console screen, but I'd prefer to be able to display it in a web page using html.

I have no idea how to approach this, so I'd be very grateful if somebody could help me.

Thanks, Tom.

The code I've got is:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
//Path of the file is assigned
TextReader tr = new StreamReader("tweets.txt");

//A dictionary object called 'list' is created
//the dictionary contains strings associated with integers (strings for the hashtags and integesr for the amount of times tweeted)
Dictionary<string, int> list = new Dictionary<string, int>();
string line = "";

//Data is read into the dictionary thath as been created
while (true)
{
line = tr.ReadLine();

//If end of file, the loop is stopped
if (line == null)
break;


if (list.ContainsKey(line))
{
//increments the integer associated with key 'line'
list[line]++;
}
else
{
list.Add(line, 1);

}

}//end of while


outputDictionaryContents(list);
Console.ReadLine();

}
catch (FileNotFoundException f)
{
Console.WriteLine(f.Message); //an exception that shows an error message if the file is not found
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message); //An general exception that returns an error message based on the error that has occured
Console.ReadLine();
}

}
// //Method for outputting the dictionary contents
private static void outputDictionaryContents(Dictionary<string, int> list)
{
//output the dictionary contents
try
{

//'Foreach Loop' outputs the total amount of hashtags in the file

int i = 0;
int numOfTags = 0;

//'OrderByDescending' makes sure the most commmon tweets are shown first
foreach (KeyValuePair<string, int> pair in list.OrderByDescending(key => key.Value))
{

numOfTags += pair.Value;


}

//This foreach loop makes sure only the top 20 tweets are shown

foreach (KeyValuePair<string, int> pair in list.OrderByDescending(key => key.Value))
{

//Outputs the tweets with the amount of times tweeted next to them
Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
i++;
if (i >= 20)
{
break;
}
//Outputs the total amount of hashtags in the file
}
Console.WriteLine("\nTotal in file: {0}", numOfTags);
}
catch (Exception e)
{
Console.WriteLine(e.Message); //An general exception that returns an error message based on the error that has occured
Console.ReadLine();
}
}
}
}

Ads

Find all posts by this user
Quote this message in a reply
10-01-2012, 12:15 PM
Post: #2
 
You need a windows web server that will allow you to run C# program.
Or you install IIS on your computer.
Now you need to create an asp.net program in an html file that will call your C# program.
Using IIS will run the program just on your computer and not on the web. But you could then investigate making your computer a web server.

Have fun.

Ads

Find all posts by this user
Quote this message in a reply
10-01-2012, 12:15 PM
Post: #3
 
***** FRONT END (ASPX) *****

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="YA_txt._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-… xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="ltlReport" runat="server" EnableViewState="false" />
</div>
</form>
</body>
</html>

***** BACKEND (CS) *****


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace YA_txt
{
public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
GenerateReport(ReadFile("tweets.txt"));
}

private Dictionary<string, int> ReadFile(string filename)
{
Dictionary<string, int> list = new Dictionary<string, int>();
try
{
TextReader tr = new StreamReader(Server.MapPath(filename));

string line = String.Empty;

while ((line = tr.ReadLine()) != null)
{
if (!list.ContainsKey(line)) list.Add(line, 0);
list[line]++;
}

tr.Close();
}
catch (Exception ex) { throw ex; }

return list;
}

private void GenerateReport(Dictionary<string, int> list)
{
StringBuilder report = new StringBuilder();
int noOfTags = 0;
foreach (KeyValuePair<string, int> kvp in list)
noOfTags += kvp.Value;

report.AppendFormat("<p>Total in file:{0}</p>", noOfTags);

foreach (KeyValuePair<string, int> kvp in list.OrderByDescending(k => k.Value).Take(20))
report.AppendFormat("<p>{0}, {1}</p>", kvp.Key, kvp.Value);

ltlReport.Text = report.ToString();
}
}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)