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.
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
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.
Can you show us an example of your code that "breaks" XML?
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"); }
Anyone can tell me why can't I use Linq here?
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(); } }}
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++.
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
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; }
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
CA Neeraj Kumar 1,882
André Arnaud de Cal... 794 Super User 2025 Season 2
Sohaib Cheema 525 User Group Leader