StreamReader Question!

Last post 11-28-2005 12:30 PM by HotChick. 6 replies.

Sort Posts:

  • Wink [;)] StreamReader Question!

    11-21-2005, 5:16 PM
    • Member
      720 point Member
    • HotChick
    • Member since 04-25-2005, 4:48 PM
    • Posts 144

    Ok, I have found this nifty piece of code.  It does a nice job of grabbing a URL and outputting it.  What I would like to do is find tags in the HTML of the URL supplied. 

    How would I look for all instances of the "<b>"   and get the text between and the "</b>" tags?

    /////////////////////////  CODE FROM AN EXAMPLE ON ANOTHER SITE  ////////////////////
    private
    String readHtmlPage(string url)
    {

    String result;
    WebResponse objResponse;
    WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
    objResponse = objRequest.GetResponse();

    using
    (StreamReader sr = new
    StreamReader(objResponse.GetResponseStream()))
    {
    result = sr.ReadToEnd();
    // Close and clean up the StreamReader
    sr.Close();
    }

    return result;
    }

    ///////////////////////////// END ////////////////////////

    Thank you,
    I am in Australia on business and will be checking the forums.  Hopefully, some of you can help a girl out.

  • Re: StreamReader Question!

    11-21-2005, 6:39 PM
    • Contributor
      3,692 point Contributor
    • eramseur
    • Member since 10-09-2004, 12:36 AM
    • Washington DC
    • Posts 752
    I would recommend getting the request and then serializing it to XML.  Then you can search through the document fast and easy and get exact in between elements.
  • Re: StreamReader Question!

    11-22-2005, 9:56 AM
    • Member
      203 point Member
    • ahmed_elnoby
    • Member since 11-17-2005, 10:29 AM
    • Egypt
    • Posts 45

    Hi ,
    you can do it splitting the value of the stream reader and storing it in a string array  by the following

    Dim Mystr() as string
    Mystr = String.split(result,"<b>")

    this will return your stream reader values as follows
    "<b>...............</b>"
    "<b>...............</b>"
    "<b>...............</b>"

    and then u can take each item individually

    thnx
    AhmedSmile [:)]

    Ahmed El Noby
    Senior Web Developer
    MCP,MCTS
  • Re: StreamReader Question!

    11-22-2005, 10:44 AM
    • Contributor
      3,585 point Contributor
    • Aidy
    • Member since 09-01-2005, 2:35 PM
    • UK
    • Posts 711

    string result = "This is some <b>bold</b> text and so is <b>this</b>";

    int posStart = 0, posEnd = 0;

    ArrayList bold = new ArrayList();

    posStart = result.IndexOf("<b>");

    while (posStart != -1)

    {

    posStart += 3;

    posEnd = result.IndexOf("</b>", posStart);

    bold.Add (result.Substring(posStart, posEnd - posStart));

    posStart = result.IndexOf("<b>", posEnd);

    }

    foreach (string text in bold)

    Response.Write (text + "<br>");

  • Re: StreamReader Question!

    11-22-2005, 10:57 AM
    • Member
      720 point Member
    • HotChick
    • Member since 04-25-2005, 4:48 PM
    • Posts 144
    Thank you, You are a Genius! 
  • Re: StreamReader Question!

    11-22-2005, 12:31 PM
    • Contributor
      3,692 point Contributor
    • eramseur
    • Member since 10-09-2004, 12:36 AM
    • Washington DC
    • Posts 752
    Yes but if you stream all the HTML out to XML ( which is very easy to do) then its already serialized and you have it in xml format.  You can then use the fast search operations to do powerful processing instead of using the streams to do ok functionality.
  • Wink [;)] Re: StreamReader Question!

    11-28-2005, 12:30 PM
    • Member
      720 point Member
    • HotChick
    • Member since 04-25-2005, 4:48 PM
    • Posts 144
    Ok, that sounds great, so how would I do it using this:

    ----------

    private void Page_Load(object sender, System.EventArgs e)

    {

    myPage.Text = readHtmlPage("http://localhost/CSharpTest/MARCRiskDetail.htm");

    }

    private String readHtmlPage(string url)

    {

    //String result;

    WebResponse objResponse;

    WebRequest objRequest = System.Net.HttpWebRequest.Create(url);

    objResponse = objRequest.GetResponse();

    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))

    {

    String result = sr.ReadToEnd();

    //string result = "This is some <b>bold</b> text and so is <b>this</b>";

    int posStart = 0, posEnd = 0;

    ArrayList bold = new ArrayList();

    posStart = result.IndexOf("<div>Time:");

    while (posStart != -1)

    {

    posStart += 0;

    posEnd = result.IndexOf(")</div>", posStart);

    bold.Add (result.Substring(posStart, posEnd - posStart));

    posStart = result.IndexOf("<div>Time:", posEnd);

    }

    foreach (string text in bold)

    Response.Write (text + "<br>");

    // Close and clean up the StreamReader

    sr.Close();

    }

    return "";

    // return result;

    }

    -----------

    Thank you!!

Page 1 of 1 (7 items)