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

Notifications

Announcements

No record found.

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
    237,795 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
    237,795 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

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans