[C#]DataTable与XML互相转换
324 Views
using System.Xml;
//使用XmlTextWriter类。
XmlTextWriter writer = new XmlTextWriter(address, Encoding.GetEncoding("GBK"));
//设置缩进值
writer.Formatting = Formatting.Indented;
//XML文档创建开始
writer.WriteStartDocument();
//填写注释:
writer.WriteComment("这是一个注释");
//开始标签:
writer.WriteStartElement("data");
//标签中的参数:
writer.WriteAttributeString("param1","value1");
//结束上一个标签:
writer.WriteEndElement();
//结束文档
writer.WriteEndDocument();
//关闭文件
writer.Close();
一开始还犯了个错误,XML格式的文件只支持这种格式
<a>
<b>
内容1
</b>
<c>
内容2
</c>
</a>
不支持这种方式
<a>
内容1
</a>
<b>
内容2
</b>
所以一般在最开始和结尾用ROOT标签包围起来保证不会出错
DataSet与XML相互转换
public static bool WriteToXml(DataSet ds, string address)
{
try
{
//如果文件DataTable.xml存在则直接删除
if (File.Exists(address))
{
File.Delete(address);
}
XmlTextWriter writer =
new XmlTextWriter(address, Encoding.GetEncoding("GBK"));
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();//XML文档创建开始
writer.WriteStartElement("ROOT"); //防止xml出错,定义一个开始的根节点
foreach (DataTable dt in ds.Tables)
{
writer.WriteComment("DataTable: " + dt.TableName);//注释
writer.WriteStartElement("data"); //开始一个datatable
writer.WriteAttributeString("TableName", dt.TableName);//data标签中添加表名
writer.WriteAttributeString("CountOfRows", dt.Rows.Count.ToString());//data标签中添加行数
writer.WriteAttributeString("CountOfColumns", dt.Columns.Count.ToString());//data标签中添加列数
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.WriteEndElement();//结束根节点
writer.WriteEndDocument();//结束文档
writer.Close();
//XML文档创建结束
}
catch (Exception ex)
{
LogUtils.writeErrorLog("【WriteToXml错误】:"+ex.ToString());
return false;
}
return true;
}
public static DataSet ReadFromXml(string address)
{
DataSet ds = new DataSet();
string err_name = "";
try
{
if (!File.Exists(address))
{
err_name = "文件不存在!";
throw new Exception("文件不存在!");
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(address);
XmlNode Root = xmlDoc.SelectSingleNode("ROOT");
XmlNodeList x = Root.ChildNodes;
foreach (XmlNode root in x)
{
if (root.Attributes == null)
continue;
DataTable dt = new DataTable();
//读取表名
string tb_name = ((XmlElement)root).GetAttribute("TableName");
//string tb_name = ((XmlElement)root).GetAttribute("TableName");
dt.TableName = tb_name;
//读取行数
int CountOfRows = 0;
if (!int.TryParse(((XmlElement)root).GetAttribute("CountOfRows").ToString(), out CountOfRows))
{
err_name = "["+tb_name+"]"+"行数转换失败";
throw new Exception("行数转换失败");
}
//读取列数
int CountOfColumns = 0;
if (!int.TryParse(((XmlElement)root).GetAttribute("CountOfColumns").ToString(), out CountOfColumns))
{
err_name = "[" + tb_name + "]" + "列数转换失败";
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("行插入成功");
}
//添加到ds中
ds.Tables.Add(dt);
}
}
catch (Exception ex)
{
LogUtils.writeErrorLog("【ReadFromXml错误】:" + ex.ToString());
err_name = ex.Message;
ds = new DataSet();
ds.DataSetName = err_name;
return ds;
}
return ds;
}