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

Adding xml node attribute in AL code.

(0) ShareShare
ReportReport
Posted on by

Hi,

I am trying to replace dotnet variable by AL functions. But am not clear how to add attribute value to XML node. below is my dotnet code.

Can anyone suggest and if possible please provide some examples.

var
XMLDomDocParam : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument"
XMLDomNode : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode"
XMLDomAttribute : DotNet "'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode";
begin
XMLDomAttribute := XMLDomDocParam.CreateAttribute(AttribName);
if ISNULL(XMLDomAttribute) then
  exit(false);

if AttribValue <> '' then
   XMLDomAttribute.Value := AttribValue;
XMLDomNode.Attributes.SetNamedItem(XMLDomAttribute);
CLEAR(XMLDomAttribute);
exit(true);
end;
Categories:
  • Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    You can do that by using XMLElement object. For example:

    NewNode := XmlElement.Create('Name', '', 'Stefano');
    NewNode.SetAttribute('ID', '1');
  • Community Member Profile Picture
    on at

    HI Stefano,

    Thanks for the reply :)

    Actually the above code is one function and the function have a xmlnode parameter with var type.

    For that xmlnode  need the attribute.

    I have tried like below.

    XmlElement2 := ParmXMLDomNode.AsXmlElement();

    XmlElement2.SetAttribute(AttribName,AttribValue);

    ParamXMLDomNode := XmlElement2.AsXmlNode();

    Is this correct? And may i know how to save this xmldocument through AL, So that i can check it ..

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

    To save the XMLDocument, you can use XmlDocument.WriteTo method:

    docs.microsoft.com/.../xmldocument-writeto-outstream-method

    Something like:

    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);

  • JJMc Profile Picture
    293 on at

    Good morning all....

    As far as XmlElement is concern, I have a "big" issue.

    When I XmlParentNode.AsXmlElement().Add a new child node, this does not add at the end of the parent node, it adds at the begining.

    Any suggestion?.

    Thank you ever so much in advance.

  • Community Member Profile Picture
    on at

    Hi ,

    Here I have tried to add parent node and added child node also to create data for Magento. Have a look if it can help.

    github.com/.../COD.70001.MagentoRequestCr.al

  • JJMc Profile Picture
    293 on at

    Ok..... It helped.....

    Solved!!!!

    Cheers.

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