{"title":"Query.SaveAsXml(Integer, OutStream) Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/query/query-saveasxml-integer-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/query/query-saveasxml-integer-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/query/query-saveasxml-integer-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":"Saves the resulting data set of a query as an .xml file.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":117,"chars":85},{"level":2,"title":"Parameters","slug":"parameters","at":202,"chars":301},{"level":2,"title":"Return Value","slug":"return-value","at":503,"chars":233},{"level":2,"title":"Remarks","slug":"remarks","at":736,"chars":156},{"level":2,"title":"Open, Read, or Close semantics with the SaveAsXML method","slug":"open-read-or-close-semantics-with-the-saveasxml-method","at":892,"chars":872},{"level":2,"title":"Example","slug":"example","at":1764,"chars":422},{"level":2,"title":"Related information","slug":"related-information","at":2186,"chars":133}],"text":"> Version: _Available or changed with runtime version 1.0._\nSaves the resulting data set of a query as an .xml file.\n## Syntax\n```al\n[Ok := ]  Query.SaveAsXml(Number: Integer, OutStream: OutStream)\n```\n## Parameters\nNumber\nType: ../integer/integer-data-type\nThe ID of the query object that you want to save as an .xml file. If the query that you specify does not exist, then a run-time error occurs.\nOutStream\nType: ../outstream/outstream-data-type\nThe stream that you want to save the query as XML to.\n## Return Value\n*[Optional] Ok*\nType: ../boolean/boolean-data-type\ntrue if the SaveAsXml succeeded, otherwise false. If you omit this optional return value and the operation does not execute successfully, a runtime error will occur.\n## Remarks\nWhen the SaveAsXML method is called, the dataset is generated as XML and streamed to the OutStream object designated by the OutStream parameter.\n## Open, Read, or Close semantics with the SaveAsXML method\nThe SaveAsXML method can be called at any place in the code and does not require that the Open, Read, or Close methods are called before it. When the SaveAsXML method is called, a new instance of the query is created. The query is implicitly opened, read, and closed. If there is currently a dataset in the opened state when the SaveAsXML method is called, then that instance is closed. This means that the following code is in-correct because the query is not open on the second Read call.\n```al\n// Not correct way to use Query.SaveAsXML\nQuery.Open;\nQuery.Read;\nQuery.SaveAsXML(myOutStream);\n// Query is now closed, so next read call will fail\nQuery.Read;\n```\nThe correct code for this example is as follows.\n```al\n// Correct way to use Query.SaveAsXML\nQuery.SaveAsXML(myOutStream);\nQuery.Open;\nQuery.Read;\n```\n## Example\nThe following example shows how to get data from a query with the name My Customer Query into an outstream for further processing.\n```al\nvar\n    MyCustomerQuery: Query \"My Customer Query\";\n    myOutStream: OutStream;\n    OK: Boolean;\n    Text000: Label 'Query was not saved.';\nbegin\n    OK := MyCustomerQuery.SaveAsXML(Query::MyCustomerQuery, myOutStream);\n    if not OK then\n          Error(Text000);\nend;\n```\n## Related information\nquery-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}