How to Read and Write Xml File in Dynamics Ax


Description:-

XmlTextWriter Class allows you to Write XML to a file. You can use the inbuilt properties and methods of the XmlTextWriter class to create an xml file. XmlTextWriter Class provides a way to generating a XML Document/File.

1. Create an XmlTextWriter Object.
2. Pass the Xml File name and encoding type parameter through the Object. If encoding parameter is set to Nothing, then default encoding will be "UTF-8".

      XmlTextWriter writer  = new XMlTextWriter("PathtoSaveFile/FileName", null);

3. Indent Xml Document: Use Formatting Property to automatically format the XML File.

      writer.Formatting = Formatting.Indented;

4. Use Following essential Methods as per your requirements.

Write Document

I. WriteStartDocument: This is the very first method of XmlTextWriter object to open new xml document with xml declaration of version 1.0.

      writer.WriteStartDocument();

IIWriteStartElement: This method adds an xml tag inside the xml file. It takes the tag name as a Input.

      writer.WriteStartElement("FirstTagName");     
      It adds the FirstTagName as a tag inside the xml file.

III. WriteStartAttribute: This method adds an attribute to the Created elements. It takes attribute name, xml namespace and prefix string as inputs. We can use xml namespace and prefix string as blank string.

      writer.WriteStartAttribute(" ", "id" " ");     
It adds the attribute 'id' to the student tag.

IV. WriteString: This method writes a text context. This can contain the value of the attribute with xml tag.

      writer.WriteString("1");                 
      It sets the id with the value "1"

V.WriteElementString: Write an element with the specified local name and value.

      writer.WriteElementStrine("Name", "Umesh");          
       It adds the Name tag with value Umesh.

VI.WriteEndAttribute: This method closes the previously added attribute. 

      writer.WriteEndAttribute();        
      It closes the attribute id.

VII. WriteEndElement: It closes one element. If more than one element is present, then it closes the inner one.

      writer.WriteEndElement();          
      It closes the Student tag.

VIII. WriteComments: This method is used to give specific comments while creating the xml file. It takes the Comment string as its input.

      writer.WriteComment("This is the xml file creation using XmlTextWriter");
      This adds the comment in the XML file.

IX. WriteEndDocument: This is the last method which is used to close the Xml document.

      writer.WriteEndDocument();   
      This closes the xml file.

In Dynamic Ax you need to add 

      writer.close();

X. Flush: This method flushes whatever is in the buffer to the underlying streams and also flushes the underlying streams.

      writer.Flush();

Write Xml File Using XmlTextWriter

static void writeXML(Args _args)
{
    XMLTextWriter xw;
    A_PurchaseOrder ObjA_PurchaseOrder;
    str text,filepath;
    container confilter =['.xml','*.xml'];
    ;
    filepath = WinAPI::getSaveFileName(
        0,
        confilter,
        "",
        "Save As",
        "",
        "Untitled");
    xw = XMLTextWriter::new File(filepath);
    xw.writeStartDocument();
    xw.writeStartElement("PurchseOrder");
    while select ObjA_PurchaseOrder
    {
        xw.writeStartElement("Data");
        xw.writeElementString("PurchaseID",ObjA_PurchaseOrder.Purchase_ID);
        xw.writeElementString("VendorCode",ObjA_PurchaseOrder.Vender_Code);
        xw.writeElementString("Status",enum2str(ObjA_PurchaseOrder.Status));
        xw.writeElementString("PurchaseDate",
                date2str(ObjA_PurchaseOrder.Purchase_Date,123,
                DateDay::Digits2,DateSeparator::Hyphen,
                DateMonth::Digits2,DateSeparator::Hyphen,
                DateYear::Digits4));
        xw.writeElementString("PurchaseAmount",int2str(ObjA_PurchaseOrder.Purchase_Amount));
        xw.writeEndElement();
    }
    xw.writeEndElement();
    xw.writeEndDocument();
    xw.close();
}

Read Xml File Using XmlDocument

static void readingXML(Args _args)
{
    str text,filepath;
    containe rconfilter =['.xml','*.xml'];
    XMLDocument xmlDoc;
    inti, nooftags;
    ;
    filepath = WinAPI::getOpenFileName(
        0,
        confilter,
        "",
        "Save As",
        "",
        "Untitled");
    xmlDoc = XMLDocument::newFile(filepath);
    nooftags = xmlDoc.getElementsByTagName("Data").length();
    for(i=0; i<nooftags; i++)
    {
        info(strfmt("%1 | %2 | %3 | %4 | %5",
            xmlDoc.getElementsByTagName("PurchaseID").item(i).text(), 
            xmlDoc.getElementsByTagName("VendorCode").item(i).text(),
            xmlDoc.getElementsByTagName("Status").item(i).text(),
            xmlDoc.getElementsByTagName("PurchaseDate").item(i).text(),
            xmlDoc.getElementsByTagName("PurchaseAmount").item(i).text()));
    }
}

Related Posts

Previous
Next Post »

Thanks for comments.....