Convert Datatable to XML String using LINQ

Convert Datatable to XML String using LINQ

protected void Page_Load(object sender, EventArgs e)  
{  
    if(!Page.IsPostBack)  
    {  
        GenerateXMLString();  
    }  
}  
protected void GenerateXMLString()  
{  
    DataTable dt = null;  
    try  
    {  
        dt = new DataTable();  
        dt.Columns.Add("ID", typeof (int));  
        dt.Columns.Add("Name", typeof (string));  
        dt.Columns.Add("Marks", typeof (int));  
        DataRow dr = dt.NewRow();  
        dr[0] = 1;  
        dr[1] = "Smith";  
        dr[2] = 200;  
        dt.Rows.Add(dr);  
        DataRow dr1 = dt.NewRow();  
        dr1[0] = 2;  
        dr1[1] = "Mark";  
        dr1[2] = 500;  
        dt.Rows.Add(dr1);  
        XDocument stud = new XDocument(new XDeclaration("1.0", "utf-8", "true"), new XElement("STUDENTS", from stu in dt.AsEnumerable() select new XElement("STUDENT", new XElement("ID", stu["ID"]), new XElement("Name", stu["Name"]), new XElement("Marks", stu["Marks"]))));  
        string _encodedXML = string.Format("<pre>{0}</pre>", HttpUtility.HtmlEncode(stud));  
        Response.Write(_encodedXML.ToString());  
    }  
    catch(Exception Ex)  
    {  
        throw Ex;  
    }  
}  

2020-02-16