Tuesday, March 14, 2023

Export XML using x++


 Export/Create XML using x++.

Output :



Here I have covered multiple scenarios.

    1. Adding a Sub tag like Name1 and Name2 under Main.

    2. Adding Header with child.

    3. Multiple child nodes are added in a header node. (PayemntList ->Payment ->Credit)

    4. Assign a value to Node.

    5. Creating Blank Tag. (SummaryChild2)


Code:

public void createXML()
{
	XmlDocument doc;
	XmlElement mainNode;
	XmlElement headerNode;
	XmlElement summaryNode;
	XmlElement oneChild;
	XmlElement secondChild;
	XmlElement SummaryChil1;
	XmlElement SummaryChil2;
	XmlElement paymentList;
	XmlElement payment;
	XmlElement credit;
	XmlElement dedit;
	XmlElement deditAmount;
	XmlElement creditAmount;

	doc = XmlDocument::newBlank();
	mainNode = doc.createElement('Main');
	mainNode.setAttribute('Name2','Test 123');
	mainNode.setAttribute('Name1','Test 456');
	doc.appendChild(mainNode);

	headerNode = doc.createElement('Header');
	mainNode.appendChild(headerNode);

	oneChild = doc.createElement('Child1');
	oneChild.appendChild(doc.createTextNode('child value'));
	headerNode.appendChild(oneChild);


	secondChild = doc.createElement('child2');
	secondChild.appendChild(doc.createTextNode('AccountName'));
	headerNode.appendChild(secondChild);

	summaryNode = doc.createElement('Summary');
	mainNode.appendChild(summaryNode);

	SummaryChil1 = doc.createElement('SummaryChild1');
	SummaryChil1.appendChild(doc.createTextNode('sum value'));
	summaryNode.appendChild(SummaryChil1);

	SummaryChil2 = doc.createElement('SummaryChild2');
	summaryNode.appendChild(SummaryChil2);
        
        // paymentList Node
	paymentList = doc.createElement('paymentList');
	mainNode.appendChild(paymentList);

	payment = doc.createElement('payment');
	paymentList.appendChild(payment);

	credit = doc.createElement('credit');
	payment.appendChild(credit);

	creditAmount = doc.createElement('creditAmount');
	creditAmount.appendChild(doc.createTextNode('100.00'));
	credit.appendChild(creditAmount);

	dedit = doc.createElement('dedit');
	payment.appendChild(dedit);

	deditAmount = doc.createElement('deditAmount');
	deditAmount.appendChild(doc.createTextNode('200.00'));
	dedit.appendChild(deditAmount);

      Info(doc.toString());

       // Save File to Local folder
       #define.filename(@'D:\AnyNew.xml')
     doc.save(#filename);       // Download file       // Way - 1       if (!System.String::IsNullOrEmpty(xml))       {
    System.IO.StreamWriter streamWriter;     System.IO.Stream stream;     stream = new System.IO.MemoryStream();     streamWriter = new System.IO.StreamWriter(stream);     streamWriter.write(xml);     streamWriter.Flush();     stream.Seek(0, System.IO.SeekOrigin::Begin);     File::SendFileToUser(stream, 'TestXML.xml'); }         // Way - 2         System.Byte[] reportBytes = new System.Byte[0](); System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); reportBytes = enc.GetBytes(doc.outerXml()); System.IO.Stream stream = new System.IO.MemoryStream(reportBytes);
File::SendFileToUser(stream, 'TestXML.xml');
}



Keep Daxing!!

No comments:

Post a Comment