CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Posts
    14

    need some assistance

    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.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: need some assistance

    ahh, read this post AFTER I replied to your other one.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Jan 2003
    Location
    Sweden
    Posts
    115

    Re: need some assistance

    Quote Originally Posted by pdpullmn612 View Post
    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.
    Code:
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured