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!
You are mixing different string encodings together, which might account for why
TStreamReaderis not able to read everything correctly.TStringStreamalso usesTEncodingin D2009+, but you are not specifying anyTEncodingtype in theTStringStreamconstructor, so it will useTEncoding.Default, which is not the same encoding asTEncoding.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:
In the specific case of loading the document into a
TMemo, you don’t need theTStringStreamorTStreamReaderat all: