Fork me on GitHub

Generate XML string [AX2012]

Apologies if the logic in produced XML file doesn’t make much sense. I use it merely as an example of some complex XML attributes and nesting.

The method below returns XML string, which later can be saved as XML file or consumed as a XML string by web services, etc.

An example of reading the data from XML string below can be found in “Read XML String” tutorial.

Expected result

<?xml version="1.0" encoding="UTF-8"?>
<XMLScript Version="2.0">
    <Project Name="Holidays">
        <Format>C:\NotesDesign\Note.jpg</Format>
        <PrintSetup>
            <Printer>PDFPrinter</Printer>
        </PrintSetup>
        <Note Date="2014/12/10" ID="N001">
            <To>Tim</To>
            <Body>Make sure all tasks are completed before holidays!</Body>
        </Note>
        <Note Date="01/01/2015" ID="N002">
            <To>Sophie</To>
            <Body>Don't forget your Christmas shopping this weekend!</Body>
        </Note>
    </Project>
</XMLScript>

Solution (can be easily converted to service)

Service data contract attribute

[DataContractAttribute]
class YourDataContract
{
    str docXML;
}

Service data member attribute

[DataMemberAttribute('XML Document')]
public str parmDocXML(str _docXML = docXML)
{
    docXML = _docXML;
    return docXML;
}

Class declaration

public class YourClass
{
    YourDataContract            dataContract;
    str                         currentDate;

    XmlDocument                 doc;
    XmlNode                     docNode;
    XmlNode                     scriptVersionNode;
    XmlAttribute                scriptVersionAttribute;
    XmlNode                     projectNode;
    XmlAttribute                projectNodeAttribute;
    XmlNode                     formatNode;
    XmlNode                     printSetupNode;
    XmlNode                     printerNode;
    XmlNode                     noteNode;
    XmlAttribute                noteNodeAttribute;
    XmlNode                     toNode;
    XmlNode                     bodyNode;
}

A method

public YourDataContract generateXML(str _projectName, str _path, str _printer, str _noteId, str _sentTo, str _msgBody)
{
    dataContract = new YourDataContract();
    currentDate = date2Str(today(), 123, DateDay::Digits2, DateSeparator::Slash, DateMonth::Digits2, DateSeparator::Slash, DateYear::Digits4);

    /// XML Version information
    doc = new XmlDocument();
    docNode = doc.createXmlDeclaration("1.0", "UTF-8", "");
    doc.appendChild(docNode);

    /// XMLScript version node
    scriptVersionNode = doc.createElement("XMLScript");
    scriptVersionAttribute = doc.createAttribute("Version");
    scriptVersionAttribute.value("2.0");
    scriptVersionNode.attributes().setNamedItem(scriptVersionAttribute);
    doc.appendChild(scriptVersionNode);

    /// XMLScript.Project node
    projectNode = doc.createElement("Project");
    projectNodeAttribute = doc.CreateAttribute("Name");
    projectNodeAttribute.Value(_projectName);
    projectNode.attributes().setNamedItem(projectNodeAttribute);
    scriptVersionNode.appendChild(projectNode);

    /// XMLScript.Project.Format node
    formatNode = doc.createElement("Format");
    formatNode.appendChild(doc.createTextNode(_path));
    projectNode.appendChild(formatNode);

    /// XMLScript.Project.PrintSetup node
    printSetupNode = doc.createElement("PrintSetup");
    projectNode.appendChild(printSetupNode);
    /// XMLScript.Command.PrintSetup.Printer node
    printerNode = doc.createElement("Printer");
    printerNode.appendChild(doc.createTextNode(_printer));
    printSetupNode.appendChild(printerNode);

    /// Add Notes
    if(_noteId != "")
    {
        // First note node
        noteNode = doc.createElement("Note");
        noteNodeAttribute = doc.createAttribute("Date");
        noteNodeAttribute.value(currentDate);
        noteNode.attributes().setNamedItem(noteNodeAttribute);
        noteNodeAttribute = doc.createAttribute("ID");
        noteNodeAttribute.value(_noteId);
        noteNode.attributes().setNamedItem(noteNodeAttribute);
        projectNode.appendChild(noteNode);
        toNode = doc.createElement("To");
        toNode.appendChild(doc.createTextNode(_sentTo));
        bodyNode = doc.createElement("Body");
        bodyNode.appendChild(doc.createTextNode(_msgBody));
        noteNode.appendChild(toNode);
        noteNode.appendChild(bodyNode);

        // Second note node
        noteNode = doc.createElement("Note");
        noteNodeAttribute = doc.createAttribute("Date");
        noteNodeAttribute.value("01/01/2015");
        noteNode.attributes().setNamedItem(noteNodeAttribute);
        noteNodeAttribute = doc.createAttribute("ID");
        noteNodeAttribute.value("N002");
        noteNode.attributes().setNamedItem(noteNodeAttribute);
        projectNode.appendChild(noteNode);
        toNode = doc.createElement("To");
        toNode.appendChild(doc.createTextNode("Sophie"));
        bodyNode = doc.createElement("Body");
        bodyNode.appendChild(doc.createTextNode("Don't forget your Christmas shopping this weekend!"));
        noteNode.appendChild(toNode);
        noteNode.appendChild(bodyNode);        
    }

    dataContract.parmDocXML(doc.outerXml());

    return dataContract;
}

A Job to test

static void generateXML(Args _args)
{
    YourClass yourClass = new YourClass();

    str xmlNote = "";
    xmlNote = yourClass.generateXML("Holidays", 
                                    @"C:\NotesDesign\Note.jpg", 
                                    "PDFPrinter", 
                                    "N001", 
                                    "Tim", 
                                    "Make sure all tasks are completed before holidays!"
                                    ).parmDocXML();

    info(strFmt("%1", xmlNote));
}

Infolog output

XML String

Comments !

links

social