TRichViewXML : SaveToStream ... LoadFromStream

General TRichView support forum. Please post your questions here
jclaes
Posts: 6
Joined: Fri Jan 23, 2009 10:49 am

TRichViewXML : SaveToStream ... LoadFromStream

Post by jclaes »

Hi,

I'm trying to copy the data from one RichViewEdit to another using RichViewXML.SaveToStream / .. LoadFromStream.

The destination RVE stays empty . How come ??


procedure TForm1.Button1Click(Sender: TObject);
var
aStream : TStringStream ;
aString : String ;
T: TXMLTree;
MainRoot, DocRoot: TXMLTag;
begin
// SaveToStream
T := TXMLTree.Create(RichViewXML1.Encoding);
try
T.Items.AddTag(RichViewXML1.RootElement, T, T);
MainRoot := T.Items[0];
MainRoot.Items.AddTag('Data', T.Items[0], T);
DocRoot := MainRoot.Items[MainRoot.Items.Count-1];
RichViewXML1.RichView := RichViewEdit1;
RichViewXML1.SaveToXML(DocRoot);
aStream := TStringStream.Create('') ;
T.SaveToStream(aStream,true);
finally
T.Free;
end;

// LoadFromStream

T := TXMLTree.Create(RichViewXML1.Encoding);
try
T.LoadFromStream(aStream);
RichViewXML2.RichView := RichViewEdit2;
MainRoot := T.Items.FindTagOfName(RichViewXML2.RootElement);
if MainRoot<>nil then
begin
DocRoot := MainRoot.Items.FindTagOfName('Data');
if DocRoot<>nil then
begin
RichViewXML2.LoadFromXML(DocRoot);
end;
end;
finally
T.Free;
end;
RichViewEdit2.Format ;

end;
Ar4i
Posts: 30
Joined: Tue Apr 24, 2007 7:03 am

Post by Ar4i »

To make your code more readable warp it in CODE tags.

To solve your problem you can try using InsertXXXFromStream instread of LoadFromXXX. At least thi method works fine with not XML RichView components.
jclaes
Posts: 6
Joined: Fri Jan 23, 2009 10:49 am

Post by jclaes »

Hi,

When I use the InsertFromStream i get a 'no root element error'
[/code]
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Try setting aStream.Position := 0 before loading from this stream.
jclaes
Posts: 6
Joined: Fri Jan 23, 2009 10:49 am

Post by jclaes »

Sorry but still not working.

here is the code

Code: Select all


procedure TForm1.Button1Click(Sender: TObject);
var 
  aStream : TStringStream ;
  aString : String ;
  T: TXMLTree;
  MainRoot, DocRoot: TXMLTag;
begin
  // SaveToStream
  T := TXMLTree.Create(RichViewXML1.Encoding);
  try
   T.Items.AddTag(RichViewXML1.RootElement, T, T);
   MainRoot := T.Items[0];
   MainRoot.Items.AddTag('Data', T.Items[0], T);
   DocRoot := MainRoot.Items[MainRoot.Items.Count-1];
   RichViewXML1.RichView := RichViewEdit1;
   RichViewXML1.SaveToXML(DocRoot);
   aStream := TStringStream.Create('') ;
   T.SaveToStream(aStream,true);
  finally
   T.Free;
  end;

// LoadFromStream
 aStream.Position := 0 ;
 RichViewXML2.InsertFromStream(aStream);

  T := TXMLTree.Create(RichViewXML1.Encoding);
  try
   T.LoadFromStream(aStream);

   RichViewXML2.RichView := RichViewEdit2;
   MainRoot := T.Items.FindTagOfName(RichViewXML2.RootElement);
    if MainRoot<>nil then
     begin
      DocRoot := MainRoot.Items.FindTagOfName('Data');
      if DocRoot<>nil then
       begin
         RichViewXML2.InsertFromStream(aStream);
       end;
    end;
   finally
   T.Free;
   end;
   RichViewEdit2.Format ;
end;

arnfinn
Posts: 9
Joined: Wed Sep 21, 2011 8:23 am

InsertFromStreamEd

Post by arnfinn »

Hi!

I have the same problem. The destenation is empty.
Code:
Source.SaveRTFToStream(Stream, False);
Stream.Position := 0;
stream.SaveToFile('C:\streamtest.txt');
Stream.Position := 0;
Dest.InsertRTFFromStreamEd(Stream);
Dest.Format;
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Please send a sample project to reproduce.
(PS: Format is not needed after InsertRTFFromStreamEd)
arnfinn
Posts: 9
Joined: Wed Sep 21, 2011 8:23 am

RichView

Post by arnfinn »

Hi!
I hvae a sample file. Wher do I send it??

Arnfinn.
arnfinn
Posts: 9
Joined: Wed Sep 21, 2011 8:23 am

Post by arnfinn »

Her is the code. I have made a DBRichView and the purpose is to copy from one to the other with the help of stream. I see that the stream has data butt the destination is empty. it is the line

Code: Select all

Dest.InsertRTFFromStreamEd(Stream);
that is the problem. Dest is empty.

Can you pleas help?

Code: Select all

procedure TForm1.cxButton2Click(Sender: TObject);
var
  ItemNo,
  Offset: Integer;
  Stream: TMemoryStream;
  Source,
  Dest:   TDBRichViewEdit;
  frm:    TForm;
  sStyle: TRVStyle;

begin
  try
    Stream := TMemoryStream.Create;

    frm    := TForm.Create(nil);

    sStyle := TRVStyle.Create(frm);

    Dest := TDBRichViewEdit.Create(frm);
    Dest.Parent := frm;
    Dest.Style := sStyle;
    Dest.DataField  := 'INFODATA';
    Dest.Datasource := dsTo;

    Source := TDBrichViewEdit.Create(frm);
    Source.Parent := frm;
    Source.Style := sStyle;
    Source.DataField  := 'INFODATA';
    Source.Datasource := dsFrom;

    if (dsTo.DataSet.Bof and dsTo.DataSet.Eof) then
    begin
      dsTo.DataSet.Insert;
    end else
    begin
      dsTo.DataSet.Edit;
    end;

    ItemNo := Dest.ItemCount -1;
    Offset := Dest.GetOffsAfterItem(ItemNo);
    Dest.SetSelectionBounds(ItemNo, Offset, ItemNo, Offset);

    Source.SaveRTFToStream(Stream, False);
    Stream.Position := 0;
    Dest.InsertRTFFromStreamEd(Stream);
    Dest.Format;

  finally
    FreeAndNil(Dest);
    FreeAndNil(Source);
    FreeAndNil(sStyle);
    FreeAndNil(frm);
    FreeAndNil(Stream);
    cxPageControl1.ActivePage := DBRichView_Dest;
  end;
end;
[/code]
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

From the first sight, the code looks correct. I need a sample project that I can compile to reproduce the problem.
arnfinn
Posts: 9
Joined: Wed Sep 21, 2011 8:23 am

Post by arnfinn »

Hi!
Give me an email address and I will send it to you.

Thx.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

richviewgmailcom
arnfinn
Posts: 9
Joined: Wed Sep 21, 2011 8:23 am

Post by arnfinn »

you got m@il.

Arnfinn
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Received. I'll check it tomorrow.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You insert RTF in Dest, but then you destroy Dest without posting to a database.
Instead of Dest.Format, call dsTo.DataSet.Post.
Post Reply