Convert container to Xml document

Class to convert container to Xml document:
class VKContainerToXml
{
    #define.UTF8("UTF-8")
    #define.Message("Message")
    
    XmlDocument doc;
    XmlElement  messageNode;
}
public void new()
{
    XmlNode docNode;
    /// XML Version information
    doc = new XmlDocument();
    docNode = doc.createXmlDeclaration("1.0", #UTF8, "");
    doc.appendChild(docNode);
    
    /// Message
    messageNode = doc.createElement(#Message);
    doc.appendChild(messageNode);
}
/// <summary>
/// Builds XML structure based on information in container
/// </summary>
/// <param name="_data">
/// Example of inbound container
/// [
///     ['tagName',  Value],
///     ['msg',     'some message'],
///     ['list',    [
///                     ['listItem1', 'value'],
///                     ['listItem2', 'value']
///                 ]]
/// ]
/// </param>
/// <param name="_startingNode">
/// XML node where to build structure
/// </param>
public void buildAnswerFromContainer(container _data, XmlNode _startingNode = messageNode)
{
    XmlNode     node;
    container   packNode;
    int         cLen = conLen(_data),
                i;

    for (i = 1; i <= cLen; i++)
    {
        packNode    = conPeek(_data, i);
        node        = doc.createElement(conPeek(packNode, 1));
        _startingNode.appendChild(node);
        if (Types::Container == typeOf(conPeek(packNode, 2)))
        {
            this.buildAnswerFromContainer(conPeek(packNode, 2), node);
        }
        else
        {
            node.appendChild(doc.createTextNode(this.anytype2str(conPeek(packNode, 2))));
        }
    }
}
public str getXmlStr()
{
    return doc.outerXml();
}
public XmlDocument parmDoc(XmlDocument _doc = doc)
{
    doc = _doc;

    return doc;
}
public str convDate2str(date _date)
{
    return date2str(_date, 321, DateDay::Digits2, DateSeparator::Hyphen, DateMonth::Digits2, DateSeparator::Hyphen, DateYear::Digits4, DateFlags::None);
}
public real convStr2num(str _num)
{
    return str2num(_num);
}
public str convNum2str(real _num)
{
    return num2str(_num, 0, 2, 1, 0);
}
public date convStr2date(str _date)
{
    return str2Date(_date, 321);
}
public anytype anytype2str(anytype _value)
{
    anytype value;

    switch(typeOf(_value))
    {
        case Types::Int64:
            value = int642str(_value);
            break;
        case Types::Integer:
        case Types::Enum:
            value = int2str(_value);
            break;
        case Types::Date:
            value = this.convDate2str(_value);
            break;
        case Types::Real:
            value = this.convNum2str(_value);
            break;
        default:
            value = any2str(_value);
    }

    return value;
}
Example of usage:
static void testContainerToXml(Args _args)
{
    VKContainerToXml    con2xml = new VKContainerToXml();
    container           con =   [
                                    ['tagName',  'Value'],
                                    ['msg',     'some message'],
                                    ['list',    [
                                                    ['listItem1', 'value'],
                                                    ['listItem2', 'value']
                                                ]]
                                ];
    str                 xmlStr;
    
    
    con2xml.buildAnswerFromContainer(con);
    xmlStr = con2xml.getXmlStr();
    abs(1);
}


 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units and "buy me a coffee" link.
Join us.

Blog Tags