microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
733 stars 243 forks source link

XmlDocument namespace prefix issue #4078

Closed nsyilmaz2 closed 5 years ago

nsyilmaz2 commented 5 years ago

Hello,

We want to create following xml , We tried two method but still getting error. Also we used XmlPort object howewer we were not able to display prefix add to root. How we can fix this problem?

image

First;

    xmlDoc := XmlDocument.Create;
    xmlDoc.SetDeclaration(xmlDeclaration.Create('1.0', 'UTF-8', ''));

    uri := 'http://www.w3.org/2001/XMLSchema-instance';
    ParentElement := XmlElement.Create('berat');
    ParentElement.Add(XmlAttribute.CreateNamespaceDeclaration('edefter', 'http://www.edefter.gov.tr'));

    ParentElement.Add(XmlAttribute.CreateNamespaceDeclaration('xsi', uri));
    ParentElement.Add(XmlAttribute.Create('schemaLocation', uri, 'http://www.edefter.gov.tr ../xsd/edefter.xsd'));

    ParentElement.Add(XmlAttribute.CreateNamespaceDeclaration('ds', 'http://www.w3.org/2000/09/xmldsig#'));
    ParentElement.Add(XmlAttribute.CreateNamespaceDeclaration('xades', 'http://uri.etsi.org/01903/v1.3.2#'));

    xmlDoc.Add(ParentElement);

    ChildElement := XmlElement.Create('xbrl');
    ChildElement.Add(XmlAttribute.CreateNamespaceDeclaration('xbrli', 'http://www.xbrl.org/2003/instance'));
    ChildElement.Add(XmlAttribute.CreateNamespaceDeclaration('gl-bus', 'http://www.xbrl.org/int/gl/bus/2006-10-25'));
    ChildElement.Add(XmlAttribute.CreateNamespaceDeclaration('gl-plt', 'http://www.xbrl.org/int/gl/plt/2006-10-25'));
    ParentElement.add(ChildElement);

    Clear(ChildElement);
    ChildElement := XmlElement.Create('accountingentries', 'gl-bus');
    ParentElement.add(ChildElement);

Second;

  procedure CreateXMLDocAndRoot()
   var
    t: Text;
    RootNode: XmlNode;
    XmlDocType: XmlDocumentType;
    TempXMLDoc: XmlDocument;
    ParentElement: XmlElement;
    ChildElement: XmlElement;
    XMLDoc: XmlDocument;
begin
    XmlDocument.ReadFrom(GetText(), TempXMLDoc);

    TempXMLDoc.GetRoot(ParentElement);
    RootNode := ParentElement.AsXmlNode;
    XMLDoc := XmlDocument.Create();
    XMLDoc.SetDeclaration(xmlDeclaration.Create('1.0', 'UTF-8', ''));

    ParentElement := XmlElement.Create('accountingEntries', 'gl-cor');
    RootNode.AsXmlElement().Add(ParentElement);

    XMLDoc.Add(RootNode);

    XMLDoc.WriteTo(t);
    Message(t);

end;

Thanks.

UfukAsci commented 5 years ago

Hello,

We've resolved the issue. Here is the code sample that generates the required xml successfully: ` codeunit 50125 MyCodeunit { trigger OnRun() var t: Text; xmlDoc: XmlDocument; ParentNode: XmlNode; ChildNode: XmlNode; begin xmlDoc := XmlDocument.Create();

    //root 
    AddElementWithPrefix(xmlDoc, 'berat', 'edefter', 'http://www.edefter.gov.tr', ParentNode);
    ParentNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('xsi', 'http://www.w3.org/2001/XMLSchema-instance'));
    ParentNode.AsXmlElement().Add(XmlAttribute.Create('schemaLocation', 'http://www.w3.org/2001/XMLSchema-instance', 'http://www.edefter.gov.tr ../xsd/edefter.xsd'));
    ParentNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('ds', 'http://www.w3.org/2000/09/xmldsig#'));
    ParentNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('xades', 'http://uri.etsi.org/01903/v1.3.2#'));
    xmlDoc.Add(ParentNode);

    //xbrl 
    AddElementWithPrefix(xmlDoc, 'xbrl', 'xbrli', 'http://www.xbrl.org/2003/instance', ChildNode);
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('gl-plt', 'http://www.xbrl.org/int/gl/plt/2006-10-25'));
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('link', 'http://www.xbrl.org/2003/linkbase'));
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('iso639', 'http://www.xbrl.org/2005/iso639'));
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('gl-bus', 'http://www.xbrl.org/int/gl/bus/2006-10-25'));
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('gl-cor', 'http://www.xbrl.org/int/gl/cor/2006-10-25'));
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('iso4217', 'http://www.xbrl.org/2003/iso4217'));
    ChildNode.AsXmlElement().Add(XmlAttribute.CreateNamespaceDeclaration('xlink', 'http://www.w3.org/1999/xlink'));
    ParentNode.AsXmlElement().Add(ChildNode);

    ParentNode := ChildNode;

    //schemaRef
    ChildNode := XmlElement.Create('schemaRef', 'http://www.xbrl.org/2003/linkbase').AsXmlNode();
    ChildNode.AsXmlElement().Add(XmlAttribute.Create('href', 'http://www.w3.org/1999/xlink', '../xsd/2006-10-25/plt/case-c-b/gl-plt-2006-10-25.xsd'));
    ChildNode.AsXmlElement().Add(XmlAttribute.Create('type', 'http://www.w3.org/1999/xlink', 'simple'));
    ParentNode.AsXmlElement().Add(ChildNode);

    //context
    ChildNode := XmlElement.Create('context', 'http://www.xbrl.org/2003/instance').AsXmlNode();
    ChildNode.AsXmlElement().Add(XmlAttribute.Create('id', 'ledger_context'));
    ParentNode.AsXmlElement().Add(ChildNode);

    ParentNode := ChildNode;
    ChildNode := XmlElement.Create('entity', 'http://www.xbrl.org/2003/instance').AsXmlNode();
    ParentNode.AsXmlElement().Add(ChildNode);

    ParentNode := ChildNode;
    ChildNode := XmlElement.Create('identifier', 'http://www.xbrl.org/2003/instance', '1234567890').AsXmlNode();
    ChildNode.AsXmlElement().Add(XmlAttribute.Create('scheme', 'http://www.gib.gov.tr'));
    ParentNode.AsXmlElement().Add(ChildNode);

    xmlDoc.WriteTo(t);
    Message(t);
end;

procedure AddElementWithPrefix(var pXMLDocument: XmlDocument; pNodeName: Text; pPrefix: Text; pNameSpace: text; var pCreatedNode: XmlNode)
begin

    pCreatedNode := XmlElement.Create(pNodeName, pNameSpace).AsXmlNode;
    pCreatedNode.AsXmlElement.Add(XmlAttribute.CreateNamespaceDeclaration(pPrefix, pNameSpace));
end;

} ` Thank you,

Ufuk Asci

martinhocosta88 commented 5 years ago

Since .net is not supported in BC online, is there any other method we can used to create a xml structure by code without using XMLDocument?

ftornero9 commented 5 years ago

Hello @martinhocosta88 ,

In the above examples they are using XmlDocument but not .NET

Regards.

martinhocosta88 commented 5 years ago

Hello @martinhocosta88 ,

In the above examples they are using XmlDocument but not .NET

Regards.

Thanks for the reply! Just realized that's not .NET. I've checked but it has not the same functions as .NET, like the .Load and .Save