XmlAttributeCollection initialization in AL

dreezdreez Member Posts: 68
edited 2020-12-07 in NAV Three Tier
Hello,

I would like to use XmlAtributeCollection, but I have no idea how to initialize it.

Here's my sample code:
var
    XmlAttrCol: XmlAttributeCollection;
begin
    XmlAttrCol.Set('attr1', 'value1');
    XmlAttrCol.Set('attr2', 'value2');
    XmlAttrCol.Set('attr3', 'value3');
    XmlAttrCol.Set('attr4', 'value4');
end;
I am getting an error:
Microsoft.Dynamics.Nav.Runtime.NavXmlAttributeCollection was not initialized.
But there is no such method to initialize XmlAttributeCollection...

I have also tried following code:
var
    XmlAttrCol: XmlAttributeCollection;
    XmlElem: XmlElement;
begin
    XmlAttrCol := XmlElem.Attributes();
    XmlAttrCol.Set('attr1', 'value1');
    XmlAttrCol.Set('attr2', 'value2');
    XmlAttrCol.Set('attr3', 'value3');
    XmlAttrCol.Set('attr4', 'value4');
end;
but with the same error...

Any help, please?

Best Answer

  • dreezdreez Member Posts: 68
    Answer ✓
    Okay, I got it...
    The thing is, that you have to create the XmlElement first, then the second variant works. so...
    var
        XmlAttrCol: XmlAttributeCollection;
        XmlElem: XmlElement;
    begin
        XmlElem := XmlElement.Create('sampleElement');
        XmlAttrCol := XmlElem.Attributes();
        XmlAttrCol.Set('attr1', 'value1');
        XmlAttrCol.Set('attr2', 'value2');
        XmlAttrCol.Set('attr3', 'value3');
        XmlAttrCol.Set('attr4', 'value4');
    end;
    

    ...works fine.

Answers

  • dreezdreez Member Posts: 68
    Answer ✓
    Okay, I got it...
    The thing is, that you have to create the XmlElement first, then the second variant works. so...
    var
        XmlAttrCol: XmlAttributeCollection;
        XmlElem: XmlElement;
    begin
        XmlElem := XmlElement.Create('sampleElement');
        XmlAttrCol := XmlElem.Attributes();
        XmlAttrCol.Set('attr1', 'value1');
        XmlAttrCol.Set('attr2', 'value2');
        XmlAttrCol.Set('attr3', 'value3');
        XmlAttrCol.Set('attr4', 'value4');
    end;
    

    ...works fine.
Sign In or Register to comment.