web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Are there any examples of reading and writing XML with the new XML data types in AL?

(0) ShareShare
ReportReport
Posted on by 20

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/xmldocument/xmldocument-data-type

I can't find a good example in the help of it all working together. 

Thanks,

Jim Welch

Categories:
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    An XML file can be ready by using:

    XmlDocument.ReadFrom(XMLStr, XmlDocument); //Here XMLStr is the Text variable that containt your XML

    To select nodes, declare a variable of type XmlNodeList and use:

    IF XmlDocument.SelectNodes('//node1/child1',XmlNodeList)

    To write an XML to a string, just use XMLDocument.WriteTo(DocumentText); //DocumentText is a Text variable

    An example on an XML "on the fly" creation with output to the end user:

    xmlDoc := xmlDocument.Create();

           xmlDec := xmlDeclaration.Create('1.0', 'UTF-8', '');

           xmlDoc.SetDeclaration(xmlDec);

           xmlElem := xmlElement.Create('root');

           xmlElem.SetAttribute('release', '2.1');

           xmlElem2 := XmlElement.Create('FirstName');

           xmlElem2.Add(xmlText.Create('Max'));

           xmlElem.Add(xmlElem2);

           xmlDoc.Add(xmlElem);

           // Create an out stream from the blob, notice the encoding.

           TempBlob.Blob.CreateOutStream(outStr, TextEncoding::UTF8);

           // Write the contents of the doc to the stream

           xmlDoc.WriteTo(outStr);

            // From the same blob, that now contains the xml document, create an instr

           TempBlob.Blob.CreateInStream(inStr, TextEncoding::UTF8);

           // Save the data of the InStream as a file.

           File.DownloadFromStream(inStr, 'Export', '', '', fileName);

  • Jim Welch Profile Picture
    20 on at

    This was very helpful.

    Thank you,

    Jim Welch

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans