Home > Forum > XML/XSL > How to generate XML in asp.net?

How to generate XML in asp.net?

by Thamil   on Oct 07, 2013   Category: XML/XSL  |  Views: 6845    |  Points: 5   |  Gold 
  Reply
Hi experts,
I want to generate an XML file from sql server Database such as one can read the data from that xml file on an specific id. how can I generate xml file in asp.net page.

With Best Regards,
Sajib Barua.

Question posted on behalf of Sajib Barua

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :How to generate XML in asp.net?   
by Thamil
on Oct 7 2013 12:17AM
Points : 10
Gold 
Hi Sajib Barua

Please use below code to achieve your requirement. here im creating xml file from datatable, assume you have a data in datatable and want to create xml file, by using Xdocument i have created xml file and it will store into application root folder.
try
{
DataTable dtEmploye = new DataTable();
dtEmploye.Columns.Add("EMPLOYEENO");
dtEmploye.Columns.Add("NAME");
dtEmploye.Columns.Add("ADDRESS");
dtEmploye.Columns.Add("PHONE");
dtEmploye.Columns.Add("REMARKS");

DataRow rw = dtEmploye.NewRow();
rw[0] = "1";
rw[1] = "RAJA";
rw[2] = "INDAI";
rw[3] = "465435345";
rw[4] = "No Remarks";
dtEmploye.Rows.Add(rw);

DataRow rw1 = dtEmploye.NewRow();
rw1[0] = "2";
rw1[1] = "JORG";
rw1[2] = "CANADA";
rw1[3] = "65434";
rw1[4] = "Test11";
dtEmploye.Rows.Add(rw1);

XDocument Employee = new XDocument(new XDeclaration("1.0", "utf-8", "true"),
new XElement("COMPNAY", from cust in dtEmploye.AsEnumerable()
select new XElement("EMPLOYEE",
new XElement("NO", cust["EMPLOYEENO"]),
new XElement("Name", cust["NAME"]),
new XElement("Address", cust["ADDRESS"]),
new XElement("Phone", cust["Phone"]),
new XElement("Remarks", cust["Remarks"])
)));

Employee.Save(Server.MapPath("test.xml"));
}
catch (Exception ex)
{
Response.Write(ex.Message);
}


Once you execute the above code, you will get below xml file in root folder.
<?xml version="1.0" encoding="utf-8"?>
<COMPNAY>
<EMPLOYEE>
<NO>1</NO>
<Name>RAJA</Name>
<Address>INDAI</Address>
<Phone>465435345</Phone>
<Remarks>No Remarks</Remarks>
</EMPLOYEE>
<EMPLOYEE>
<NO>2</NO>
<Name>JORG</Name>
<Address>CANADA</Address>
<Phone>65434</Phone>
<Remarks>Test11</Remarks>
</EMPLOYEE>
</COMPNAY>
  Re :How to generate XML in asp.net?   
by Harish
on Jan 31 2014 4:41AM
Points : 10
Starter 
Read Below article to create your own sitemap.xml file..

http://stepcoder.com/Articles/10009/how-to-create-dynamic-sitemap-xml-file-from-database-in-asp-net

Hope this Helps.

Recent Post

Latest Posts