How to remove a child node from an XML file in dynamics 365 finance and operations using x++

0

How to remove a child node from an XML file in dynamics 365 finance and operations using x++

In Microsoft Dynamics 365 Finance and Operations (D365 FO), you can use the below x++ codes to remove a child node from an XML file. If you want to remove all child nodes of a parent node in an XML file you can use RemoveAll() method. You can copy and paste the below codes and change them as per your requirement. Here's an example of how you can achieve this:


...

Xml File Format

------------------------------------

<?xml version="1.0" encoding="UTF-8">

<FirstElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ThirdElement xsi:type="Attribute">

</FirstElement>

-------------------------------------

internal final class CodingSpiderCodingExamples

{  

   public static void main(Args _args)

   {

       FileIoPermission permission;

        XMLDocument xmlDoc = XMLDocument::newBlank();

        XMLNode rootNode;

        XMLNode Node1, Node2;

        XMLElement xmlElement;

        str ns = 'http://www.w3.org/2001/XMLSchema-instance'; 

        permission= new FileIoPermission('C:\\MyFile.xml','w');

        permission.assert();

        xmlDoc = XMLDocument::newBlank(); 

        //Parent node

        rootNode = xmlDoc.documentElement();

        xmlElement = xmlDoc.createElement('FirstElement');

        xmlElement.setAttribute('xmlns:xsi',ns);

        rootNode = xmlDoc.appendChild(xmlElement); 

        //Child node 1

        xmlElement = xmlDoc.createElement('SecondElement');

        xmlElement.setAttribute2('type', ns, 'Attribute');

        Node1 = rootNode.appendChild(xmlElement); 

        //Child node 2

        xmlElement = xmlDoc.createElement('ThirdElement');

        xmlElement.setAttribute2('type', ns, 'Attribute');

        Node2 = rootNode.appendChild(xmlElement);

       //Remove all child nodes

        rootNode.removeAll(); 

        // Save the file

        xmldoc.save('C:\\MyFile.xml');

   }

Output file

---------------------

<?xml version="1.0" encoding="UTF-8">

<FirstElement ></FirstElement>

...


Here root nod is the "FirstElement " node , other two nodes are child nodes, after executing the x++ codes , both cild noded will be removed from the xml file and you can see the output file.

Post a Comment

0 Comments
Post a Comment
>
To Top