DataTable 与XML 交互

一、将DataTable的内容写入到XML文件中

/// <summary>

/// 将DataTable的内容写入到XML文件中

/// </summary>

/// <param name="dt">数据源</param>

/// <param name="address">XML文件地址</param>

public static bool WriteToXml(DataTable dt, string address)

{

try

{

//如果文件DataTable.xml存在则直接删除

if (File.Exists(address))

{

File.Delete(address);

}

XmlTextWriter writer =

new XmlTextWriter(address, Encoding.GetEncoding("GBK"));

writer.Formatting = Formatting.Indented;

//XML文档创建开始

writer.WriteStartDocument();

writer.WriteComment("DataTable: " + dt.TableName);

writer.WriteStartElement("DataTable"); //DataTable开始

writer.WriteAttributeString("TableName", dt.TableName);

writer.WriteAttributeString("CountOfRows", dt.Rows.Count.ToString());

writer.WriteAttributeString("CountOfColumns", dt.Columns.Count.ToString());

writer.WriteStartElement("ClomunName", ""); //ColumnName开始

for (int i = 0; i < dt.Columns.Count; i++)

{

writer.WriteAttributeString(

"Column" + i.ToString(), dt.Columns[i].ColumnName);

}

writer.WriteEndElement(); //ColumnName结束

//按行各行

for (int j = 0; j < dt.Rows.Count; j++)

{

writer.WriteStartElement("Row" + j.ToString(), "");

//打印各列

for (int k = 0; k < dt.Columns.Count; k++)

{

writer.WriteAttributeString(

"Column" + k.ToString(), dt.Rows[j][k].ToString());

}

writer.WriteEndElement();

}

writer.WriteEndElement(); //DataTable结束

writer.WriteEndDocument();

writer.Close();

//XML文档创建结束

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

return false;

}

return true;

}

二、将以上述格式在XML中保存的信息读取到DataTable内

/// <summary>

/// 从XML文件中读取一个DataTable

/// </summary>

/// <param name="dt">数据源</param>

/// <param name="address">XML文件地址</param>

/// <returns></returns>

public static DataTable ReadFromXml(string address)

{

DataTable dt = new DataTable();

try

{

if (!File.Exists(address))

{

throw new Exception("文件不存在!");

}

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(address);

XmlNode root = xmlDoc.SelectSingleNode("DataTable");

//读取表名

dt.TableName = ((XmlElement)root).GetAttribute("TableName");

//Console.WriteLine("读取表名: {0}", dt.TableName);

//读取行数

int CountOfRows = 0;

if (!int.TryParse(((XmlElement)root).

GetAttribute("CountOfRows").ToString(), out CountOfRows))

{

throw new Exception("行数转换失败");

}

//读取列数

int CountOfColumns = 0;

if (!int.TryParse(((XmlElement)root).

GetAttribute("CountOfColumns").ToString(), out CountOfColumns))

{

throw new Exception("列数转换失败");

}

//从第一行中读取记录的列名

foreach (XmlAttribute xa in root.ChildNodes[0].Attributes)

{

dt.Columns.Add(xa.Value);

//Console.WriteLine("建立列: {0}", xa.Value);

}

//从后面的行中读取行信息

for (int i = 1; i < root.ChildNodes.Count; i++)

{

string[] array = new string[root.ChildNodes[0].Attributes.Count];

for (int j = 0; j < array.Length; j++)

{

array[j] = root.ChildNodes[i].Attributes[j].Value.ToString();

}

dt.Rows.Add(array);

//Console.WriteLine("行插入成功");

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

return new DataTable();

}

return dt;

}

三、Main函数调用

static void Main(string[] args)

{

//1.将DataTable内容写入到XML文件

//建立DataTable取名Table4Test

DataTable dt1 = new DataTable("Table4Test");

//生成四列,列名为Column0-3

dt1.Columns.Add("Column0");

dt1.Columns.Add("Column1");

dt1.Columns.Add("Column2");

dt1.Columns.Add("Column3");

//生成四行

dt1.Rows.Add("A0", "A1", "A2", "A3");

dt1.Rows.Add("B0", "B1", "B2", "B3");

dt1.Rows.Add("C0", "C1", "C2", "C3");

//写入到XML

if (WriteToXml(dt1, "DataTable.xml"))

{

Console.WriteLine("写入成功");

}

//================================

//2.从XML文件中读取内容到DataTable

//读取信息

DataTable dt2 = ReadFromXml("DataTable.xml");

//输出读取的信息

Console.WriteLine("读取到的信息:");

for (int i = 0; i < dt2.Rows.Count; i++)

{

for (int j = 0; j < dt2.Columns.Count; j++)

{

Console.Write(dt2.Rows[i][j].ToString() + "\t");

}

Console.WriteLine();

}

Console.ReadLine();

}

 

posted @ 2020-04-25 13:48  安以痕_陈  阅读(256)  评论(0编辑  收藏  举报