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

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to insert data into a XML schema and sign it with xades

(0) ShareShare
ReportReport
Posted on by 170

I have a task to insert data into a XML schema, it is always the same.

How should I declare it? How to insert by code data in the right places?

Secondly how can I sign it with xades? I found in AOT a class for it but I don't know where in AX we can use it to sign XML files.

I have the same question (0)
  • Suggested answer
    huijij Profile Picture
    19,811 on at
    RE: How to insert data into a XML schema and sign it with xades

    Hi RadekM,

    Please refer to the example to insert elements into the xml file:

    docs.microsoft.com/.../insert-xml-dml

    Microsoft Dynamics 365 Finance supports the XAdES-BES signature format. You may follow these steps to enable Finance to support FatturaPA:

    docs.microsoft.com/.../emea-ita-e-invoices

  • RadekM Profile Picture
    170 on at
    RE: How to insert data into a XML schema and sign it with xades

    Thank you, but it's not really helping me.

    I'm looking for a way to do it in X++ and System.XML but I have a problem since I don't know how to get the elements right.

    I'm loading the xml file as a preset, I can add new elements and values for them. But the moment I want add a value to a existing elements the xml 'breaks' and it delete most of data that was there.

    What I need to arviche is to add values to certain elements and be able to delete te elements on condition since the preset is very basic and I don't need some elements all the time.

  • Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to insert data into a XML schema and sign it with xades

    Can you show us an example of your code that "breaks" XML?

  • RadekM Profile Picture
    170 on at
    RE: How to insert data into a XML schema and sign it with xades

    The bad thing wa to reassign doc.documentelement and add the innertext to it. After that I had only the xmlns links, and the created element data..

     public static void Main(Args _args)
        {
            DocumentHistoryParameters  DocumentHistoryParameters = DocumentHistoryParameters::find();
            str path = DocumentHistoryParameters.FilePath;  
            boolean fileexists;
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    
            //If there is no current file, then create a new one
            fileExists = WinAPIServer::fileExists(path);
            if (!fileExists)
            {
                //Create neccessary nodes
                System.Xml.XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                System.Xml.XmlComment comment = doc.CreateComment("This is an XML Generated File");
                doc.AppendChild(declaration);
                doc.AppendChild(comment);
           }
            else //If there is already a file
            {
                //    //Load the XML File
                doc.Load(path);
            }
    
            //Get the root element
            System.Xml.XmlElement root = doc.DocumentElement;
            
            System.Xml.XmlElement Subroot = doc.CreateElement("Expences");
            System.Xml.XmlElement Date1 = doc.CreateElement("Date");
            System.Xml.XmlElement Amount = doc.CreateElement("Amount");
            System.Xml.XmlElement Purpose = doc.CreateElement("purpose");
            System.Xml.XmlElement Source = doc.CreateElement("Source");
            System.Xml.XmlElement Bank = doc.CreateElement("Bank");
         //   System.Xml.XmlElement NIP;
            System.Xml.XmlElement PelnaNazwa = doc.DocumentElement;
            System.Xml.XmlElement Podmiot1 = doc.SelectSingleNode("Podmiot1");
            System.Xml.XmlElement DaneIdentyfikacyjne = doc.SelectSingleNode("DaneIdentyfikacyjne");
            //  System.Xml.XmlNodeList PelnaNazwa = doc.GetElementsByTagName("PelnaNazwa");
            
            //Add the values for each nodes
            Date1.InnerText = "date";
            Amount.InnerText = "amount";
            Purpose.InnerText = "purpose";
          //  NIP.innertext = "123123123123";
            PelnaNazwa.innertext = "azazazaza";
            Podmiot1.innertext = "123123123123";
            DaneIdentyfikacyjne.innertext = "123123123123";
    
            Subroot.AppendChild(Date1);
            Subroot.AppendChild(Amount);
            //Subroot.AppendChild(Bank);
            root.AppendChild(Subroot);
            doc.AppendChild(root);
    
            //Save the document
            doc.Save(path);
            //Show confirmation message
            info("Details  added Successfully");
        }

  • RadekM Profile Picture
    170 on at
    RE: How to insert data into a XML schema and sign it with xades

    Anyone can tell me why can't I use Linq here?

    pastedimage1644841037122v1.png

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    class EInvoiceApiConnect
    {
        public static void Main(Args _args)
        {
            DocumentHistoryParameters  DocumentHistoryParameters = DocumentHistoryParameters::find();
            str path = DocumentHistoryParameters.FilePath;
          
            {
                System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load(path);
                System.Xml.Linq.XElement id = doc.Descendants("ID").FirstOrDefault();
            }
      }}

  • Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to insert data into a XML schema and sign it with xades

    FirstOrDefault() is a generic method.

    In C#, you can call it without the type parameter if the compiler is able to do it for you, but there is nothing like that in X++.

    Because the support for generics in X++ is very limited, a better idea is writing such code in a C# class library and call this library from X++.

  • RadekM Profile Picture
    170 on at
    RE: How to insert data into a XML schema and sign it with xades

    Good to know but I think I will pass on writing libaries for now.

    Could you tell me how can I make this call on item[ ?

    I'm getting errors invalid token '.' after the [

    System.Xml.XmlElement root = doc.DocumentElement.FirstChild;
    
            root["SystemInfo"].innertext = "zzz";
            if (root.HasChildNodes)
            {
                for (int i=0; i

  • Verified answer
    Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: How to insert data into a XML schema and sign it with xades

    You could use root.ChildNodes.Item(i), for example.

    But you don't need to care about these indexes at all if you use an enumetor:

    System.Collections.IEnumerator enumerator = root.ChildNodes.GetEnumerator();
    while (enumerator.MoveNext())
    {
        System.Xml.XmlNode child = enumerator.Current;
    }

  • RadekM Profile Picture
    170 on at
    RE: How to insert data into a XML schema and sign it with xades

    Alright... somehow I got to the point where all the data is put correct where I want it.

    The next step should be easy but its not. I need to delete from XML the childs that have empty strings. Yet when I run my code first time all childs are still in my XML and they are empty just as it represent my table. BUt on second run... the childs get the text "22222222222" thats not supposed to work this way, somehow it sees it like this childs were a tier higer that it was previous. Running the code more times it just repeats these 2 patterns: empty string, "2222222"; 

     System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(path);
            System.Xml.XmlElement root = doc.DocumentElement.FirstChild;
            root = root.NextSibling; // pomijam nagłówek
    
            do
            {
                System.Collections.IEnumerator newenumerator = root.ChildNodes.GetEnumerator();
                while (newenumerator.MoveNext())
                {
                    System.Xml.XmlNode newchild = newenumerator.Current;
                    if(newchild.HasChildNodes)
                    {
                        System.Collections.IEnumerator newenumerator2 = newchild.ChildNodes.GetEnumerator();
                        while (newenumerator2.MoveNext())
                        {
                            System.Xml.XmlNode newchild2 = newenumerator2.Current;
                            if(newchild2.HasChildNodes)
                            {
                                System.Collections.IEnumerator newenumerator3 = newchild2.ChildNodes.GetEnumerator();
                                while (newenumerator3.MoveNext())
                                {
                                    System.Xml.XmlNode newchild3 = newenumerator3.Current;
                                    if(newchild3.HasChildNodes)
                                    {
                                        System.Collections.IEnumerator newenumerator4 = newchild3.ChildNodes.GetEnumerator();
                                        while (newenumerator4.MoveNext())
                                        {
                                            System.Xml.XmlNode newchild4 = newenumerator4.Current;
                                            Global::Info(newchild4.ParentNode.Name);
                                            newchild4.InnerText = "444444444444444";
    
                                        }
                                    }
                                    else
                                    {
                                        newchild3.InnerText = EinvoiceXMLHeaderTable::findXMLFieldValue(newchild3.ParentNode.LocalName);
                                        newchild2.RemoveChild(newchild3);
                                        if(newchild3.innertext == " " || newchild3.InnerText == "")
                                        {
                                           // newchild2.RemoveChild(newchild3);
                                        }
                                       // Global::Info(newchild3.ParentNode.Name);
                                    }
                                }
                            }
                            else
                            {
                                newchild2.InnerText = "222222222222";
                                Global::Info(newchild2.ParentNode.Name);
                            }
                        }
                    }
                    else
                    {
                        newchild.InnerText = "11111111111";
                        Global::Info(newchild.ParentNode.Name);
                    }
                }
    
                if(root.NextSibling == null)
                {
                    end = true;
                }
                else
                {
                    root = root.NextSibling;
                }
            }
            while(!end);

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,882

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 794 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 525 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans