Home > DynamicsAx Utilities > De-serialization of string format XML in Dynamics Ax

De-serialization of string format XML in Dynamics Ax


For some requirements, we need to store XML as a string in some Ax-Tables and need to de-serialize this string field as a document at a later point of time.

During de-serialization process need to store the XML values in to some tables.

The following example will illustrate, how XML string can be read using X++ code.

static void PSReadXMLFileAsString(Args _args)
{

    XmlTextReader       xmlTextReader ;
    str                 xmlString = ‘<ATGSubmitOrder >’
                                    +'<ATGAXSTGCustTable action="create">’
                                    +'<ATGOrderId>o130006</ATGOrderId>’
                                    +'<ATGProfileId>260005</ATGProfileId>’
                                    +'</ATGAXSTGCustTable>’
                                    +'</ATGSubmitOrder>’;
   
    ;

    xmlTextReader = XmlTextReader::newXml(xmlString, true) ;

    //xmlTextReader.read() ;

    while(xmlTextReader.read())
    {
        switch (xmlTextReader.NodeType())
            {
                case XmlNodeType::Element: // The node is an element.
                    print ("<" + xmlTextReader.Name() + ">");
                    break;
                case XmlNodeType::Text: //Display the text in each element.
                    print (xmlTextReader.Value());
                    break;
                case XmlNodeType::EndElement: //Display the end of the element.
                    print ("</" + xmlTextReader.Name() + ">");
                    break;
            }
    }
     pause;

}

Reading the XML string with attributes as well

static void PSReadXMLFileAsStringAttr(Args _args)
{

    XmlTextReader       xmlTextReader ;
    str                 xmlString = ‘<ATGSubmitOrder xmlns="
http://schemas.microsoft.com/dynamics/2008/01/documents/ATGSubmitOrder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">’
                                    +'<ATGAXSTGCustTable action="create" Id="PS">’
                                    +'<ATGOrderId>o130006</ATGOrderId>’
                                    +'<ATGProfileId>260005</ATGProfileId>’
                                    +'</ATGAXSTGCustTable>’
                                    +'</ATGSubmitOrder>’;

    str                 xmlNodeAttributes;

    ;

    xmlTextReader = XmlTextReader::newXml(xmlString, true) ;

    while(xmlTextReader.read())
    {
        switch (xmlTextReader.NodeType())
            {
                case XmlNodeType::Element: // The node is an element.
                    xmlNodeAttributes = "<" + xmlTextReader.Name();
                   
                    while (xmlTextReader.MoveToNextAttribute()) // Read the attributes.
                     xmlNodeAttributes += " " + xmlTextReader.Name() + "=’" + xmlTextReader.Value() + "’";

                    xmlNodeAttributes += ">";
                    print xmlNodeAttributes;
                    break;
                case XmlNodeType::Text: //Display the text in each element.
                    print (xmlTextReader.Value());
                    break;
                case XmlNodeType::EndElement: //Display the end of the element.
                    print ("</" + xmlTextReader.Name() + ">");
                    break;
            }
    }
     pause;

}

Sarcastic smile

Advertisements
Categories: DynamicsAx Utilities
  1. Nitesh
    July 23, 2010 at 8:23 am

    Thanks Santosh

  2. Santosh Kumar
    July 23, 2010 at 2:01 pm

    U r most welcome Sir ji… 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: