Using X++ code create and Read XML file.

As in Dynamics AX 2009, XML is widely used , so I got a code which will help to create XML file.

static void Jimmy_CreateXMLFile(Args _args)
{
    
//As in Dynamics AX 2009, XML is widely used , so I got a code which will help to create XML file.
    XmlDocument    doc;
    XmlElement     nodeXml;
    XmlElement     nodeTable;
    XmlElement     nodeAccount;
    XmlElement     nodeName;
    LedgerTable    ledgerTable;
    filename       filename;
    
int            i;
    #WinAPI
;
    doc        
= XmlDocument::newBlank();
    nodeXml    
= doc.createElement('xml');
    doc.appendChild(nodeXml);

    
while select ledgerTable order by ledgerTable.AccountNum
    {
        i
++;
        nodeTable   
= doc.createElement(tablestr(LedgerTable));
        nodeTable.setAttribute(fieldstr(LedgerTable, RecId),int642str(ledgerTable.RecId));

        nodeXml.appendChild(nodeTable);

        nodeAccount 
= doc.createElement(fieldstr(LedgerTable, AccountNum));
        nodeAccount.appendChild(doc.createTextNode(ledgerTable.AccountNum));
        nodeTable.appendChild(nodeAccount);

        nodeName    
= doc.createElement(fieldstr(LedgerTable, AccountName));
        nodeName.appendChild(doc.createTextNode(ledgerTable.AccountName));
        nodeTable.appendChild(nodeName);
        
if(i == 5)
            
break;
    }

    filename 
= WinAPI::getFolderPath(#CSIDL_PERSONAL);
    filename 
+= '\\accounts.xml';
    doc.save(filename);
    info(
"Finished!");
}

 

Using DataSet read XML file

 

static void Jimmy_DataSetReadXML(Args _args)
{
    System.Data.DataSet                 DataSet;
    System.Data.DataTableCollection     DataTableCollection ;
    System.Data.DataTable               DataTable;
    System.Data.DataRowCollection       DataRowCollection;
    System.Data.DataColumnCollection    DataColumnCollection;
    System.Data.DataRow                 DataRow;
    System.Data.DataColumn              DataColumn;
    
int                                 i,j,TotalCol,TotalRow;
    str                                 _xml;
;
    DataSet                 
= new System.Data.DataSet();
    DataSet.ReadXml(
@"D:\My Documents\accounts.xml");
    DataTableCollection     
= DataSet.get_Tables();
    DataTable               
= DataTableCollection.get_Item(0);
    DataColumnCollection    
= DataTable.get_Columns();//
    DataRowCollection       = DataTable.get_Rows();//
    TotalRow                = DataRowCollection.get_Count();//行数
    TotalCol                = DataColumnCollection.get_Count();//列数
    for(i = 0; i < TotalRow; i ++)
    {
        DataRow 
= DataRowCollection.get_Item(i);
        
//setprefix(CLRInterop::getAnyTypeForObject(DataRow.get_Item(i)));
        for(j = 0; j < TotalCol; j ++)
        {
            
//DataRow = DataRowCollection.get_Item(i);
            if (DataRow.IsNull(j))
                _xml 
= 'null';
            
else
                _xml 
= DataRow.get_Item(j);
            info(strfmt(
"%1 - %2 - %3",i,j,_XML));
        }
    }
}

 

原文地址:https://www.cnblogs.com/Fandyx/p/2116111.html