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)

How to make a node to be a child node of the other one

(0) ShareShare
ReportReport
Posted on by 110

Hello!

I want to make the #InvoiceItems node to be child node of #HANG_HOA_DONS but i do not know how to do. Please help me. Here's my code. Thanks a lot!

xmlRecord1 = xmlDoc.createElement("HANG_HOA_DONS");
        while select invoiceDataTmp where invoiceDataTmp.DataId == invoiceHeaderDataTmp.HeaderId
        {
            xmlRecord = xmlDoc.createElement(#InvoiceItems);

            for (u=2; u<= 10/*dTable1.fieldCnt()*/; u++)
        {
            // Get the fieldId from the field-count
            fieldId1 = dTable1.fieldCnt2Id(u);
            // Find the DictField object that matches the fieldId
            dField1 = dTable1.fieldObject(fieldId1);
            // Skip system fields
            if (dField1.isSystem())
                continue;
            // Create a new XmlElement (field) and
            // have the name equal to the name of the
            // dictField

                xmlField = xmlDoc.createElement(dField1.label());
            // Convert values to string. I have just added

            // a couple of conversion as an example.
            // Use tableName.(fieldId) instead of fieldname
            // to get the content of the field.
            switch (dField1.baseType())
            {
                case Types::Int64 :
                value1 = int642str(invoiceDataTmp.(fieldId1));
                break;
                case Types::Integer :
                value1 = int2str(invoiceDataTmp.(fieldId1));
                break;
                default :
                value1 = invoiceDataTmp.(fieldId1);
                break;
            }
            // Set the innerText of the XmlElement (field)
            // to the value from the table
            xmlField.innerText(value1);
            // Append the field as a child node to the record

            xmlRecord.appendChild(xmlField);
        }
            



        // Add the record as a child node to the root
        xmlRoot.appendChild(xmlRecord);

        
        }
    
       xmlRoot.appendChild(xmlRecord1);


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Heinz Schweda Profile Picture
    1,367 on at

    Hello, i don't know where and how xmlRoot is defined, but maybe you have to change your last lines like this

               // Add the record as a child node to the root

               xmlRecord1.appendChild(xmlRecord);

           }

          xmlDoc.appendChild(xmlRecord1);

  • Nguyen Do Huy Profile Picture
    110 on at

    Hello Heinz!

    while select invoiceHeaderDataTmp
    
       {
    
           // Create a new object of the XmlDocument class
    
           xmlDoc = XmlDocument::newBlank();
    
           // Create the root node
    
           xmlRoot = xmlDoc.createElement(#InvoiceRootNode);
    
           // Loop through all the records in the carTable
    
           SubTotal = 0;
    
           TotalVAT = 0;
    
           GrandTotal = 0;
    
           // Create a XmlElement (record) to hold the
    
           // contents of the current record.
    
           xmlRecord = xmlDoc.createElement(#InvoiceRecords);
    
           // Loop through all the fields in the record
    
           for (i=1; i<=12/*dTable.fieldCnt()*/; i++)
    
           {
    
               // Get the fieldId from the field-count
    
               fieldId = dTable.fieldCnt2Id(i);
    
               // Find the DictField object that matches the fieldId
    
               dField = dTable.fieldObject(fieldId);
    
               // Skip system fields
    
               if (dField.isSystem())
    
                   continue;
    
               // Create a new XmlElement (field) and
    
               // have the name equal to the name of the
    
               // dictField
    
               xmlField = xmlDoc.createElement(dField.label());
    
               // Convert values to string. I have just added
    
               // a couple of conversion as an example.
    
               // Use tableName.(fieldId) instead of fieldname
    
               // to get the content of the field.
    
               switch (dField.baseType())
    
               {
    
                   case Types::Int64 :
    
                   value = int642str(invoiceHeaderDataTmp.(fieldId));
    
                   break;
    
                   case Types::Integer :
    
                   value = int2str(invoiceHeaderDataTmp.(fieldId));
    
                   break;
    
                   default :
    
                   value = invoiceHeaderDataTmp.(fieldId);
    
                   break;
    
               }
    
               // Set the innerText of the XmlElement (field)
    
               // to the value from the table
    
               xmlField.innerText(value);
    
               // Append the field as a child node to the record
    
               xmlRecord.appendChild(xmlField);
    
           }
    
           // Add the record as a child node to the root
    
           xmlRoot.appendChild(xmlRecord);


    Here is my above code. I'm trying to get record from 2 tables. But i just do not know how to make #InvoiceItems nodes to be the field of HANG_HOA_DON node

  • Nguyen Do Huy Profile Picture
    110 on at

    please check my response below. thanks a lot !

  • Heinz Schweda Profile Picture
    1,367 on at

    Why didn't you change your code as suggested? From my point of view you use the appendChild() wrong.

  • Nguyen Do Huy Profile Picture
    110 on at

    I have tried to change my code as you said and it just take the last record of #InvoiceItems

  • Nguyen Do Huy Profile Picture
    110 on at

    And I want the HANG_HOA_DONS node take all the records of #InvoiceItems

  • Verified answer
    Heinz Schweda Profile Picture
    1,367 on at

    Hello,

    i don't have your tables and/or fields in my system, so i'm not able to reproduce exactly your scenario. But i tried to write a similar job for my system, and for me it works as expected:

    static void Job1(Args _args)

    {

       XmlDocument xmlDoc;

       XmlElement xmlRecord1, xmlRecord, xmlField;

       CustGroup custGroup;

       counter u;

       #Tax_AT

       xmlDoc = XmlDocument::newBlank(#encoding);

       xmlRecord1 = xmlDoc.createElement("HANG_HOA_DONS");

       while select custGroup

       {

           xmlRecord = xmlDoc.createElement("InvoiceItems");

           for (u=1; u<=5; u++)

           {

               xmlField = xmlDoc.createElement("Field_"+int2str(u));

               xmlField.innerText("Some value");

               // Append the field as a child node to the record

               xmlRecord.appendChild(xmlField);

           }

           // Add the record as a child node to the root

           xmlRecord1.appendChild(xmlRecord);

       }

       xmlDoc.appendChild(xmlRecord1);

       info(xmlDoc.xml());

    }

    The created XML looks like this:

    <?xml version="1.0" encoding="iso-8859-1"?>

    <HANG_HOA_DONS>

    <InvoiceItems>

        <Field_1>Some value</Field_1>

        <Field_2>Some value</Field_2>

        <Field_3>Some value</Field_3>

        <Field_4>Some value</Field_4>

        <Field_5>Some value</Field_5>

    </InvoiceItems>

    <InvoiceItems>

        <Field_1>Some value</Field_1>

        <Field_2>Some value</Field_2>

        <Field_3>Some value</Field_3>

        <Field_4>Some value</Field_4>

        <Field_5>Some value</Field_5>

    </InvoiceItems>

    <InvoiceItems>

        <Field_1>Some value</Field_1>

        <Field_2>Some value</Field_2>

        <Field_3>Some value</Field_3>

        <Field_4>Some value</Field_4>

        <Field_5>Some value</Field_5>

    </InvoiceItems>

    </HANG_HOA_DONS>

    Maybe you will show us, how your XML should look like by posting an example?

  • Nguyen Do Huy Profile Picture
    110 on at

    Yes thank you. I have solved problem by changing the code as you suggested. Thanks again.

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