My program won't display all the text on a webpage, it basically only reads the first line. I am using a rich textbox to display the text.

void readrss()
        {
            try
            {
                newstxt.Text = string.Empty;
                StreamReader x = new StreamReader(WebRequest.Create("http://www.xample.net/").GetResponse().GetResponseStream());
                newstxt.Text += x.ReadLine();
                x.Close();
                newstxt.Text = newstxt.Text.Replace("<", "•");
            }
            catch { }

        }

Dani AI

Generated

As pointed out, the symptom comes from only grabbing a single line. Beyond that quick fix, a more robust approach is: fetch the full response, parse the HTML to extract visible text (not tags), decode HTML entities, and update the UI safely. The snippet below shows a modern, safe pattern you can use from a background task and then marshal results back to the UI thread.

using System.Net.Http;
using HtmlAgilityPack;
using System.Net;

async Task<string> FetchPageTextAsync(string url)
{
    using var client = new HttpClient();
    var html = await client.GetStringAsync(url);
    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    var body = doc.DocumentNode.SelectSingleNode("//body") ?? doc.DocumentNode;
    var visible = body.InnerText;
    return WebUtility.HtmlDecode(visible).Trim();
}

Steps and practical tips:

  • Use the Html Agility Pack to strip tags and access only text nodes instead of regex. See Html Agility Pack homepage for installation and docs (Html Agility Pack).
  • Decode HTML entities with WebUtility.HtmlDecode (safer than manual Replace). Microsoft docs: .
  • Don’t swallow exceptions with empty catch { }; log or surface errors so you can see HTTP failures or parsing problems.
  • If the page builds content client-side with JavaScript, the raw HTML won’t include it. For those pages use a headless browser (Playwright or Selenium) to render and then extract document.body.innerText (see Playwright docs: Playwright for .NET).
  • When updating a RichTextBox from an async/background operation, marshal to the UI thread (Invoke/BeginInvoke) and use AppendText or set Text/Rtf appropriately so large content displays correctly.

These changes will make the extractor reliable and easier to debug compared with the original single-line read approach used by .

Recommended Answers

All 2 Replies

Thats because you're only reading one line. newstxt.Text += x.ReadLine(); reads a single line from the stream.
Use either x.ReadToEnd or use a while loop to itereate through each line with x.ReadLine. If you use a loop you will need to look at the x.EndOfStream property.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.