locked
Convert datatable to xml and save in a folder RRS feed

  • Question

  • User-1888080761 posted

    Hi,

    How can I convert datable to xml and save the xml file to a folder path?

    Monday, November 16, 2015 12:40 AM

Answers

  • User1724605321 posted

    Hi vignesht5,

    How can I convert datable to xml and save the xml file to a folder path?

    If you have the DataTable already you can use its WriteXml method to save the datatable into an Xml file.The WriteXml method accepts a string argument that is the file location of where you want to save the file to. Code below is for your reference:

                DataTable tblDatas = new DataTable("Datas");
                DataColumn dc = null;
                dc = tblDatas.Columns.Add("Att1", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("Att2", Type.GetType("System.String"));
    
                DataRow newRow;
                newRow = tblDatas.NewRow();
                newRow["Att1"] = "ATT1";
                newRow["Att2"] = "ATT1.1";
                tblDatas.Rows.Add(newRow);
    
                newRow = tblDatas.NewRow();
                newRow["Att1"] = "ATT2";
                newRow["Att2"] = "ATT2.1";
                tblDatas.Rows.Add(newRow);
    
                newRow = tblDatas.NewRow();
                newRow["Att1"] = "ATT3";
                newRow["Att2"] = "ATT3.1";
                tblDatas.Rows.Add(newRow);
    
                newRow = tblDatas.NewRow();
                newRow["Att1"] = "ATT4";
                newRow["Att2"] = "ATT4.1";
                tblDatas.Rows.Add(newRow);
    
    
                DataSet dataSet = new DataSet();
                dataSet.Tables.Add(tblDatas);
    
                // Save to a file
                dataSet.WriteXml(@"D:\MyData.xml");

    You could also refer to link below for more details:
    http://forums.asp.net/t/1441971.aspx?How+can+i+convert+my+datatable+into+XML+using+C+2+0+ .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 16, 2015 1:36 AM