{"title":"XmlDocument.WriteTo(OutStream) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/xmldocument/xmldocument-writeto-outstream-method/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/xmldocument/xmldocument-writeto-outstream-method, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/xmldocument/xmldocument-writeto-outstream-method","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Serializes and saves the current node to the given variable.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":121,"chars":72},{"level":2,"title":"Parameters","slug":"parameters","at":193,"chars":238},{"level":2,"title":"Return Value","slug":"return-value","at":431,"chars":238},{"level":2,"title":"Example","slug":"example","at":669,"chars":1394},{"level":2,"title":"Related information","slug":"related-information","at":2063,"chars":139}],"text":"> Version: _Available or changed with runtime version 1.0._\nSerializes and saves the current node to the given variable.\n## Syntax\n```al\n[Ok := ]  XmlDocument.WriteTo(OutStream: OutStream)\n```\n## Parameters\nXmlDocument\nType: xmldocument-data-type\nAn instance of the xmldocument-data-type data type.\nOutStream\nType: ../outstream/outstream-data-type\nThe OutStream to which you want to save the serialized representation of the node.\n## Return Value\n*[Optional] Ok*\nType: ../boolean/boolean-data-type\ntrue if the operation was successful; otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.\n## Example\nThe following example illustrates how to create a Stream from a Blob and write to a Stream from an XML document.\n```al\npageextension 50100 CustomerListExt extends \"Customer List\"\n{\n    trigger OnOpenPage();\n    var\n        xmlDoc: XmlDocument;\n        xmlDec: XmlDeclaration;\n        xmlElem: XmlElement;\n        xmlElem2: XmlElement;\n        TempBlob: Record TempBlob Temporary;\n        outStr: OutStream;\n        inStr: InStream;\n        TempFile: File;\n        fileName: Text;\n    begin\n        xmlDoc := xmlDocument.Create();\n        xmlDec := xmlDeclaration.Create('1.0', 'UTF-8', '');\n        xmlDoc.SetDeclaration(xmlDec);\n\n        xmlElem := xmlElement.Create('root');\n        xmlElem.SetAttribute('release', '2.1');\n\n        xmlElem2 := XmlElement.Create('FirstName');\n        xmlElem2.Add(xmlText.Create('Max'));\n\n        xmlElem.Add(xmlElem2);\n        xmlDoc.Add(xmlElem);\n\n        // Create an outStream from the Blob, notice the encoding.\n        TempBlob.CreateOutStream(outStr, TextEncoding::UTF8);\n\n        // Write the contents of the doc to the stream\n        xmlDoc.WriteTo(outStr);\n\n         // From the same Blob, that now contains the XML document, create an inStr\n        TempBlob.CreateInStream(inStr, TextEncoding::UTF8);\n\n        // Save the data of the InStream as a file.\n        File.DownloadFromStream(inStr, 'Export', '', '', fileName);\n    end;\n}\n```\n## Related information\nxmldocument-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}