• Source
    1. using System;
    2. using System.Data;
    3.  
    4.  
    5. namespace SaveDatasetAsXML
    6. {
    7. static class Program
    8. {
    9. static void Main(string[] args)
    10. {
    11. /* Tạo bảng Student*/
    12. var tableStudent = new DataTable("Students");
    13. tableStudent.Columns.Add("StuID", typeof(int));
    14. tableStudent.Columns.Add("FirstName", typeof(string));
    15. tableStudent.Columns.Add("LastName", typeof(string));
    16. tableStudent.Columns.Add("BirthDay", typeof(DateTime));
    17.  
    18. tableStudent.PrimaryKey = new[] { tableStudent.Columns[0] }; // Thiết lập khóa
    19.  
    20. /* Nhập dữ liệu*/
    21. tableStudent.Rows.Add(new object[] { 1, "Tuan", "Le Minh", "26/06/1999" });
    22. tableStudent.Rows.Add(new object[] { 2, "Yen", "Nguyen Hai", "05/09/2000" });
    23. tableStudent.Rows.Add(new object[] { 3, "Toan", "Pham Khanh", "05/07/1998" });
    24. tableStudent.Rows.Add(new object[] { 4, "Binh", "Hoang Cong", "05/07/1999" });
    25. tableStudent.Rows.Add(new object[] { 5, "Tung", "Le khanh", "04/06/1997" });
    26. tableStudent.AcceptChanges();
    27.  
    28. /*Thêm bảng vừa tạo vào dataset*/
    29. var dataset1 = new DataSet("DataSetName");
    30. dataset1.Tables.Add(tableStudent);
    31.  
    32. /* Xuất dữ liệu ra file XML*/
    33. dataset1.WriteXml("TenFile.xml");
    34.  
    35. Console.ReadKey();
    36. }
    37. }
    38. }
    39.