Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Remove XML node

Posted on by 115

Hi All,

I want to remove following node from XML, how can I do this please?

Want to remove this part

<DocuRef class = "entity">
<Notes>test notes</Notes>
</DocuRef>

Full XML

<?xml version="1.0" encoding="utf-8"?>
<SalesOrder xmlns = "schemas.microsoft.com/.../SalesOrder">
<SalesTable class = "entity">

<DocuRef class = "entity">
<Notes>test notes</Notes>
</DocuRef>

</SalesTable>
</SalesOrder>

Result should be 

<?xml version="1.0" encoding="utf-8"?> 
<SalesOrder xmlns = "schemas.microsoft.com/.../SalesOrder"> 
<SalesTable class = "entity">

</SalesTable>

</SalesOrder>

 

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 225,490 Super User on at
    RE: Remove XML node

    I would prefer some simpler code, like this:

    XmlDocument doc = XmlDocument::newXml(xml);
    XmlElement salesTable, docuRef;
    
    XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.nameTable());
    nsManager.addNamespace("so", "schemas.microsoft.com/.../SalesOrder");
        
    salesTable = doc.selectSingleNode("so:SalesOrder/so:SalesTable", nsManager);
    docuRefElement = salesTable.selectSingleNode("so:DocuRef", nsManager);  
    salesTable.removeChild(docuRefElement);
  • AX QA Profile Picture
    AX QA 115 on at
    RE: Remove XML node

    Thanks Faisal I will try and will update you.

  • AX QA Profile Picture
    AX QA 115 on at
    RE: Remove XML node

    Thanks Martin - I will check

  • Verified answer
    Faisal Fareed Profile Picture
    Faisal Fareed 10,794 User Group Leader on at
    RE: Remove XML node

    Following method does the trick for me - I quickly tried

    private str removeXMLTag(str xml)

    {

       XmlDocument         doc = new XmlDocument();

       XmlElement             nodeScript;

       XmlNode                 parentNode, childNode;

       XmlNodeList            xmlScriptList,

                                       parentNodeList,

                                       childNodeList;

       int                 i,j;

       doc.loadXml(xml);

       // Get the root element and its child nodes

       nodeScript = doc.getNamedElement("SalesOrder");

       xmlScriptList = nodeScript.childNodes();

       for(i=0; i < xmlScriptList.length(); i++)

       {

           parentNode = xmlScriptList.item(i);

           childNodeList = parentNode.childNodes();

           for (j=0; j < childNodeList.length(); j++)

           {

               childNode = childNodeList.item(j);

               if (childNode.selectSingleNode("Notes"))

               {

                   parentNode.removeChild(childNode);

               }

           }

       }

       return doc.outerXml();

    }

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 225,490 Super User on at
    RE: Remove XML node

    One of possible solutions is loading the whole XML string to an XMLDocument object, finding SalesTable element and DocuRef element and then calling removeChild(). You can use xml() method to convert the modified object back to string.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans