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;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.