How to read a XMLstring from an API as a stream lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- arneolav · 2018-03-30 21:08 From an external API I'm getting a XML file using Synapse, Oauth1/GetBody. The file is saved to disk (as a string,  Textfile) and then read the diskfile: Using TXMLConfig: var  iDoc    : TXMLDocument;   Child,  gChild:TDOMNode;  ...  ReadXMLFile(iDoc,  FilePath);       Child := iDoc.DocumentElement.FirstChild;       while Assigned(Child) do       begin  ..... This works great, but to avoid use of diskfile  I 'v spend some hours to figure out how to read the input from the API as a stream. I suppose I have to replace the "ReadXMLFile"  but can't find "how to". May be somebody can give me an idea. ---------------------------------------------------------------------- molly · 2018-03-30 21:44 var   stream: Tmemorystream; ...   stream :=Tmemorystream.create;   // use whatever you want to fill the stream, LoadFromfile as example   stream.LoadFromFile(FilePath)   ReadXMLFile(iDoc,  stream);   Child := iDoc.DocumentElement.FirstChild;   stream.free;   // etc. ... See xmlread.pp: Quoteprocedure ReadXMLFile(out ADoc: TXMLDocument; const AFilename: String); overload; procedure ReadXMLFile(out ADoc: TXMLDocument; var f: Text); overload; procedure ReadXMLFile(out ADoc: TXMLDocument; f: TStream); overload; procedure ReadXMLFile(out ADoc: TXMLDocument; f: TStream; const ABaseURI: String); overload; ---------------------------------------------------------------------- arneolav · 2018-03-31 10:38 Hi molly! Thanks for your insperation! I'v been away from Lazarus som period and my head is "emty" of "lazarus stuff". Here is my "working" solution: function ConvXMLstring(const XMLstr: string)          // String from the API var   iDoc    : TXMLDocument;   Child, gChild:TDOMNode;   AStream: TStringStream; begin   AStream := TStringStream.Create(XMLstr);          { assign the XMLstring to the stream}   try     if Assigned(AStream) then       ReadXMLFile(iDoc, AStream);                   { read the stream, the XMLstring }       Child := iDoc.DocumentElement.FirstChild;     {Get first child }   while Assigned(Child) do .... As a help for others, here is a more complete example: ---------------------------------------------------------------------- edgarrod71 · 2018-04-24 08:50 Molly is ok, but here's another option, instead of loading it to the Stream, we can load it directly from the TXMLDocument, and paste it to the Stream, so we can use the stream with different controls using only memory (so it'll be faster). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const archivo='c:\where_the_file_is.xml';    [...]    if assigned(cStream) then        FreeAndNil(cStream);       cStream := TStringStream.Create('', CP_UTF8);      ReadXMLFile(cDoc, archivo);    WriteXML(cDoc, cStream);      cStream.Seek(0, soBeginning);      try        Memo2.Clear;        Memo2.Lines.LoadFromStream(cStream);    except        on EFOpenError do          ;  /// swallow the error...      end;      cNodo := cDoc.FirstChild;      Memo1.Lines.Clear;      Memo1.Lines.AddText(cDoc.DocumentElement.TextContent);      [...] ---------------------------------------------------------------------- arneolav · 2018-04-24 10:32 Quote from: edgarrod71 on April 24, 2018, 10:50:13 am Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      if assigned(cStream) then            FreeAndNil(cStream);                <---  Got an error on this      if assigned(cStream) then cStream := nil; <---- This compile and run OK ---------------------------------------------------------------------- arneolav · 2018-04-24 10:54 I ended up with same solution as shown by edgarrod71 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // To make it more simple I tried to get the string direct from the API GetBody:AStream := TStringStream.Create(Utf8ToAnsi(FConnect.GetBody));   <-- did not work // so I have to pass the recieved string to a function or procedure:  .....   ReadAPIstring(FConnect.GetBody);     function ReadAPIstring(Const XMLstring: string);  var  AStream: TStringStream; begin    if assigned(AStream) then AStream := nil;    AStream := TStringStream.Create(Utf8ToAnsi(XMLstring)); ......