Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8455401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:16:44+00:00 2026-06-10T12:16:44+00:00

I have a problem with using streams. I would like to read my html

  • 0

I have a problem with using streams. I would like to read my html code line by line. With reading file line by line i have no problems but i need to read actualy opened document with webbrowser so i write this:

procedure TForm2.SpeedButton2Click(Sender: TObject);
var
    iall : IHTMLElement;
    strumien : TStringStream;
    reader : TStreamReader;
begin
  if Assigned(WebBrowser1.Document) then
    begin
      iall := (WebBrowser1.Document AS IHTMLDocument2).body;
      while iall.parentElement <> nil do
        begin
          iall := iall.parentElement;
        end;
      Strumien:=Tstringstream.Create(iall.innerHTML);
      Strumien.Position:=0;
      reader:=TStreamReader.Create(Strumien, TEncoding.UTF8);
      reader.OwnStream;
      while not reader.EndOfStream do
        memo1.Lines.Add(reader.ReadLine);
    end;
end;

This code doesnt work. Reads only few lines from center of document and gives “List index out of bounds” Anyone know why? Using Embarcadero XE2 Delphi

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:16:45+00:00Added an answer on June 10, 2026 at 12:16 pm

    You are mixing different string encodings together, which might account for why TStreamReader is not able to read everything correctly. TStringStream also uses TEncoding in D2009+, but you are not specifying any TEncoding type in the TStringStream constructor, so it will use TEncoding.Default, which is not the same encoding as TEncoding.UTF8. So you are taking the original UTF-16 encoded HTML string, converting it to the OS default Ansi encoding, and then trying to read it back as UTF-8. That can only work if the data does not contain any non-ASCII characters in it.

    Try this instead:

    procedure TForm2.SpeedButton2Click(Sender: TObject);   
    var   
      iall : IHTMLElement;   
      iparent : IHTMLElement;   
      strumien : TStringStream;   
      reader : TStreamReader;   
      s: String;
    begin   
      if Assigned(WebBrowser1.Document) then   
      begin   
        iall := (WebBrowser1.Document AS IHTMLDocument2).body;   
        iparent := iall.parentElement;
        while iparent <> nil do
        begin
          iall := iparent;
          iparent := iparent.parentElement;   
        end;
        Strumien := TStringStream.Create(iall.innerHTML, TEncoding.UTF8, False);   
        try
          Strumien.Position := 0;
          reader := TStreamReader.Create(Strumien, TEncoding.UTF8);   
          try
            while not reader.EndOfStream do   
            begin
              s := reader.ReadLine;
              // use s as needed...
            end; 
          finally
            reader.Free;
          end;
        finally
          Strumien.Free;
        end;
      end;   
    end;   
    

    In the specific case of loading the document into a TMemo, you don’t need the TStringStream or TStreamReader at all:

    procedure TForm2.SpeedButton2Click(Sender: TObject);   
    var   
      iall : IHTMLElement;   
      iparent : IHTMLElement;   
    begin   
      if Assigned(WebBrowser1.Document) then   
      begin   
        iall := (WebBrowser1.Document AS IHTMLDocument2).body;   
        iparent := iall.parentElement;
        while iparent <> nil do
        begin
          iall := iparent;
          iparent := iparent.parentElement;   
        end;
        Memo1.Text := iall.innerHTML;   
      end;   
    end;   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code for reading files with float numbers on line stored like
i have problem using LIKE structure in DB2 : for example: select * from
I have a problem using JSON and arrays. Here is my code: while($row =
i have a problem using the Catch Clipboard Events code found on this link
Problem: I would like to download 100 files in parallel from AWS S3 using
I have an NSData that I would like to read as an NSInputStream. This
I have a WCF service where I would like to send a log file
I have problem using arabic font for iOS. All fonts have the same render,
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have a problem using a local SQL Server CE database with C# and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.