Click to See Complete Forum and Search --> : need some assistance


pdpullmn612
November 28th, 2008, 01:41 PM
Hey guys,

I am writing a dictionary program that looks to Meriam Webster for the definition of a word after the user inputs one and then displays it in the console. I was able to access Meriam Webster and strip most of the HTML code. However, I need to rid of anything extra. I want just the dfinition displayed in the console window. Here is my code so far,

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest HttpWReq;
string word;
string url;
Stream receiveStream;
StreamReader readStream;
string line;
while (true)
{
Console.WriteLine("Please enter a word:");
word = Console.ReadLine();
url = "http://www.merriam-webster.com/dictionary/" + word;
try
{
HttpWReq = (HttpWebRequest)WebRequest.Create(url);

HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
receiveStream = HttpWResp.GetResponseStream();
readStream = new StreamReader(receiveStream);

line = readStream.ReadLine();
while (line != null)
{
line = Regex.Replace(line, @"<(.|\n)*?>", string.Empty);
Console.WriteLine(line);
line = readStream.ReadLine();
}

readStream.Close();
HttpWResp.Close();
}
catch (Exception ex)
{
Console.WriteLine("caught exception");
}

}
}
}
}

I noticed in the HTML code that there is a specific set up of the tags before and after the definition. Is there anyway I can just display the information between those HTML tags?

Thanks in advanced guys.

rliq
December 1st, 2008, 05:31 PM
ahh, read this post AFTER I replied to your other one.

Cyanide
December 2nd, 2008, 10:55 AM
I noticed in the HTML code that there is a specific set up of the tags before and after the definition. Is there anyway I can just display the information between those HTML tags?

Thanks in advanced guys.
Well your code does most of the work allready. As you noticed, you just need to keep track of when you loop past the specific lines that mark the start and end of the area are interested in, then you just display these lines you are interested in.
line = readStream.ReadLine();
bool displayLine = false;

while (line != null)
{

if (line.Contains("Main Entry:"))
displayLine = true;

if (line.Contains("<!--word_definition-->"))
displayLine = false;

line = Regex.Replace(line, @"<(.|\n)*?>", string.Empty);

if (displayLine)
Console.WriteLine(line);

line = readStream.ReadLine();
}
--
Please rate my post if it was of any assistance to you!