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)
Suggested Answer

How to remove XML Attributes from code?

(0) ShareShare
ReportReport
Posted on by 6,478

Hi,

How can I remove attributes from XML nodes using code?

I tried xmlElement.removeAttribute('atrributeName'); and also removeAllAtribute(); but when file is generated the attributes are still there.

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Can you please show us a simple piece of code demonstrating the problem?

  • Suggested answer
    Hariharans87 Profile Picture
    3 on at

    I tried to read the xml and remove the attributes. It is working fine my side. Please check the below code and troubleshoot your issue.

    static void Hari_TestRmoveXMLAttribute(Args _args)

    {

       #define.node('INFORMATION')

       XmlDocument xmlDocument;

       XmlNode     xmlInformationNode;

       XmlNodeList xmlInformationsNodeList;

       XmlNodeList xmlChildNodeList;

       int         i;

       int         j;    

       Filename    fileName;

       XmlElement  firstName;

       XMLWriter   xmlWriter;

       XmlDocument xmlDoc;

       fileName                = @'D:\Test.xml';

       xmlDocument             = xmlDocument::newFile(fileName);

       xmlInformationsNodeList = xmlDocument.documentElement().selectNodes(#node);

       setPrefix("@SYS98689");

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

       {

           xmlChildNodeList = xmlInformationsNodeList.item(i).childNodes();

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

           {

               xmlInformationNode = xmlChildNodeList.item(j);

               firstName = xmlInformationNode;

               firstName.removeAttribute('EMPID');

           }        

       }

       xmlWriter = XMLWriter::newFile(@'d:\Test1.xml');

       xmlDocument.writeTo(xmlWriter);

    }

    Before:

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

    <INFORMATIONS>

     <INFORMATION>

    <FIRSTNAME EMPID="1">HARI</FIRSTNAME>

    <LASTNAME>S</LASTNAME>

     </INFORMATION>

    </INFORMATIONS>

    After:

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

    <INFORMATIONS>

    <INFORMATION>

    <FIRSTNAME>HARI</FIRSTNAME>

    <LASTNAME>S</LASTNAME>

    </INFORMATION>

    </INFORMATIONS>

  • Johnny Profile Picture
    6,478 on at
    Hi Martin,
    below is the classDeclaration with macros in it and methods thats create parent and child elements which has problem.

    //class declaration
    #define.Dec('ns2:Dec') #define.Dec_VATNumber('VATNumber') #define.Dec_Name('Name') #define.Dec_Street('Street') #define.Dec_PostCode('PostCode') #define.Dec_City('City') #define.Dec_CountryCode('CountryCode') #define.Dec_EmailAddress('EmailAddress') #define.Dec_Phone('Phone') //========================== #define.Rep_Representative('ns2:Rep') #define.Rep_RepId('RepID') #define.Rep_IssuedBy('issuedBy') #define.Rep_IdentificationType('identificationType') #define.Rep_NVAT('NVAT') #define.Rep_Name('Name') #define.Rep_Street('Street') #define.Rep_PostCode('PostCode') #define.Rep_City('City') #define.Rep_CountryCode('CountryCode') #define.Rep_EmailAddress('EmailAddress') #define.Rep_Phone('Phone') //=========================== #define.XmlNs2('xmlns:ns2') #define.Ns2('http://www.webSite.com/page') #define.XmlNs('xmlns') #define.Dec_IntraListingNbr('ns2:IntraListingsNbr')
    #define.ParNode('ns2:Parent')

    //method to create parent element

    private XmlElement createParentElement(XmlDocument _xmlDoc, str _parElementName)
    {
        XmlElement      xmlRootElement;
        ;
        //xmlRootElement = _xmlDoc.createElement(_parElementName);
        xmlRootElement = _xmlDoc.createElement2(_parElementName, #Ns2);
        xmlRootElement.setAttribute(#XmlNs, 'www.website.com/inputPage');
        xmlRootElement.setAttribute(#XmlNs2, #Ns2);
    
        xmlRootElement.setAttribute(#Dec_IntraListingNbr, '1');
    
        _xmlDoc.appendChild(xmlRootElement);
    
        return xmlRootElement;
    }


    // method to create child nodes for Dec

    private void createDecInfoElement(
        XmlDocument _xmlDoc,
        XmlElement _xmlDecElement,
        str _elementName,
        str _elementValue)
    {
        XmlElement  xmlElement;
        ;
        if (_elementValue)
        {
            xmlElement = _xmlDoc.createElement(_elementName);
            xmlElement.text(_elementValue);
            _xmlDecElement.appendChild(xmlElement);
        }
    }


    
    

    // method to create RepInfo element

    private XmlElement createRepInfoElement(
        XmlDocument _xmlDoc,
        XmlElement _xmlRepElement,
        str _elementName,
        str _elementValue)
    {
        XmlElement  xmlElement;
        ;
        if (_elementValue)
        {
            xmlElement = _xmlDoc.createElement(_elementName);
            xmlElement.text(_elementValue);
            _xmlRepElement.appendChild(xmlElement);
        }
    
        return xmlElement;
    }


    Here is the generated XML file with code above which has empty xmlns="" attribute that I don't want to see in generated file.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <ns2:Parent IntraListingsNbr="1" xmlns="www.website.com/inputPage" xmlns:ns2="http://www.webSite.com/page" >
    <ns2:Rep>
    <Name xmlns="">Name xxx</Name>
    <Street xmlns="">Street xxx</Street>
    <PostCode xmlns="">PostCode xxx</PostCode>
    <City xmlns="">City xxx</City>
    <CountryCode xmlns="">Country code xxx</CountryCode>
    <EmailAddress xmlns="">xxx@mail.com</EmailAddress>
    <Phone xmlns="">+555555</Phone>
    </ns2:Rep>
    <ns2:Intra SequenceNumber="1" DecReference="0120" ClientsNbr="1" AmountSum="100.00">
    <ns2:Dec>
    <Name xmlns="">Name yyy</Name>
    <Street xmlns="">Street yyy</Street>
    <PostCode xmlns="">PostCode yyy</PostCode>
    <City xmlns="">City yyy</City>
    <CountryCode xmlns="">CountryCode yyy</CountryCode>
    <EmailAddress xmlns="">yyy@mail.com</EmailAddress>
    <Phone xmlns="">+4444444</Phone>
    </ns2:Dec>
    </ns2:Intra>
    </ns2:Parent>

    in createParentElement method if I uncomment

    xmlRootElement = _xmlDoc.createElement(_parElementName);

    and comment

    //xmlRootElement = _xmlDoc.createElement2(_parElementName, #Ns2);

    then generated file looks like below. Here empty xmlns='' attributes are removed, but also 'ns2' prefix is removed from Parent node and it is not ok. 

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <Parent IntraListingsNbr="1" xmlns="www.website.com/inputPage" xmlns:ns2="http://www.webSite.com/page" >
    <ns2:Rep>
    <Name>Name xxx</Name>
    <Street>Street xxx</Street>
    <PostCode>PostCode xxx</PostCode>
    <City>City xxx</City>
    <CountryCode>Country code xxx</CountryCode>
    <EmailAddress>xxx@mail.com</EmailAddress>
    <Phone>+555555</Phone>
    </ns2:Rep>
    <ns2:Intra SequenceNumber="1" DecReference="0120" ClientsNbr="1" AmountSum="100.00">
    <ns2:Dec>
    <Name>Name yyy</Name>
    <Street>Street yyy</Street>
    <PostCode>PostCode yyy</PostCode>
    <City>City yyy</City>
    <CountryCode>CountryCode yyy</CountryCode>
    <EmailAddress>yyy@mail.com</EmailAddress>
    <Phone>+4444444</Phone>
    </ns2:Dec>
    </ns2:Intra>
    </Parent>

    =======================

    This is the output that I want to see. Parent node with 'ns2' prefix and child nodes without xmlns='' empty attributes

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <ns2:Parent IntraListingsNbr="1" xmlns="www.website.com/inputPage" xmlns:ns2="http://www.webSite.com/page" >
    <ns2:Rep>
    <Name>Name xxx</Name>
    <Street>Street xxx</Street>
    <PostCode>PostCode xxx</PostCode>
    <City>City xxx</City>
    <CountryCode>Country code xxx</CountryCode>
    <EmailAddress>xxx@mail.com</EmailAddress>
    <Phone>+555555</Phone>
    </ns2:Rep>
    <ns2:Intra SequenceNumber="1" DecReference="0120" ClientsNbr="1" AmountSum="100.00">
    <ns2:Dec>
    <Name>Name yyy</Name>
    <Street>Street yyy</Street>
    <PostCode>PostCode yyy</PostCode>
    <City>City yyy</City>
    <CountryCode>CountryCode yyy</CountryCode>
    <EmailAddress>yyy@mail.com</EmailAddress>
    <Phone>+4444444</Phone>
    </ns2:Dec>
    </ns2:Intra>
    </ns2:Parent>

  • Johnny Profile Picture
    6,478 on at

    Hi Hariharan,

    I have also tried removeAttribute() but it doesn't work in my case, probably because xmlns is empty

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    It doesn't look like a very simple example. Please try it with a single element. You'll either get a simple test case or you'll know that it actually works and the problem is somewhere else.

  • Johnny Profile Picture
    6,478 on at

    do you mean to try it with createelement and not with createElement2?

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    No, I mean that you example is too complex - the problem is hiding somewhere in a bunch of unrelated things. That's likely the main reason why you can't figure out what's wrong.

    It's also not easy for others to copy and run it, therefore a lot of people won't bother because you ask them for wasting too much time.

  • Sergiy Gr. Profile Picture
    5 on at

    First, get URI for the blank namespace:

    NamespaceMgr.LookupNamespace('', NameSpaceUri);
    here NameSpaceUri is a text variable.

    Then create elements using this namespace URI: 

    Element := XmlElement.Create('elementname', NameSpaceUri);

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