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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

Remove XML node

(0) ShareShare
ReportReport
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>

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    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.

  • Verified answer
    Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    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();

    }

  • AX QA Profile Picture
    115 on at

    Thanks Martin - I will check

  • AX QA Profile Picture
    115 on at

    Thanks Faisal I will try and will update you.

  • Suggested answer
    Martin Dráb Profile Picture
    239,022 Most Valuable Professional on at

    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);

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans