سلام دوست عزیز ،
شما میتوانید با استفاده از دو متد WriteXml و ReadXml هم ساختار و هم داده جدول را در فایل XML ذخیره نمایید ، به این مثال از MSDN توجه کنید :
private static void DemonstrateReadWriteXMLDocumentWithStream()
{
DataTable table = CreateTestTable("XmlDemo");
PrintValues(table, "Original table");
// Write the schema and data to XML in a memory stream.
System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
table.WriteXml(xmlStream, XmlWriteMode.WriteSchema);
// Rewind the memory stream.
xmlStream.Position = 0;
DataTable newTable = new DataTable();
newTable.ReadXml(xmlStream);
// Print out values in the table.
PrintValues(newTable, "New table");
}
private static DataTable CreateTestTable(string tableName)
{
// Create a test DataTable with two columns and a few rows.
DataTable table = new DataTable(tableName);
DataColumn column = new DataColumn("id", typeof(System.Int32));
column.AutoIncrement = true;
table.Columns.Add(column);
column = new DataColumn("item", typeof(System.String));
table.Columns.Add(column);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table.NewRow();
row["item"] = "item " + i;
table.Rows.Add(row);
}
table.AcceptChanges();
return table;
}
private static void PrintValues(DataTable table, string label)
{
// Display the contents of the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
Console.Write("\t{0}", row[column]);
}
Console.WriteLine();
}
}
به طور مثال هنگام کار با وب سرویس ها ، هنگامیکه شما در حال کار با پروتکل HTTP برای انتقال داده هستید ، نوع مورد نظر شما در دات نت حتما" باید اینترفیس ISerializable را Implement نماید و به عبارتی قابل تبدیل به فرمت XML باشد که DataTable این اینترفیس را Implement مینماید و هنگان انتقال ابتدا به فرمت XML درآمده و در اصطلاح Serialize میشود و در سوی دیگر جهت استفاده مجددا" به یک شیء DataTable تبدیل میگردد یا به عبارتی Deserialize میگردد ،