Archive for the ‘XML’ Category

Return data from datatable into xml format

We can return data stored in datatable into xml format by below ways.

1) As XmlDocument

Make datatable values in to xml string format and then convert it into xml document element.

strxml = “<?xml version=’1.0′ encoding=’utf-8′ ?>” +
“<DATA>” +
“<EMPNO>” + DT.ROWS[0][“EMPNO”] + “</EMPNO>” +

“<EMPNAME>” + DT.ROWS[0][“EMPNAME”] + “</EMPNAME>” +

“</DATA>”;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strxml);
return xmlDoc.DocumentElement;

2) As XML String

Make datatable values in to xml string format and then convert it into xml document element.

strxml = “<?xml version=’1.0′ encoding=’utf-8′ ?>” +
“<DATA>” +
“<EMPNO>” + DT.ROWS[0][“EMPNO”] + “</EMPNO>” +

“<EMPNAME>” + DT.ROWS[0][“EMPNAME”] + “</EMPNAME>” +

“</DATA>”;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strxml);
return xmlDoc.OuterXml;