將DataTable的資料儲存到XML中
阿新 • • 發佈:2019-02-01
/// <summary>
/// 讀取XML中的資料
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string StrPath = AppDomain.CurrentDomain.BaseDirectory + "\\DBConnect.xml";
string Astr = File.ReadAllText(StrPath, Encoding.GetEncoding("gb2312"));
DataTable Atbl = XmlToDataTable(Astr);
}
/// <summary>
/// 將Data Table的資料寫入到XML中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
DataTable Atb = new DataTable("Stu");
Atb.Columns.Add("Name");
Atb.Columns.Add("Sex");
DataRow Adr = Atb.NewRow();
Adr["Name"] = "高曉宇";
Adr["Sex"] = "22";
Atb.Rows.Add(Adr);
Adr = Atb.NewRow();
Adr["Name"] = "高小牛";
Adr["Sex"] = "1";
Atb.Rows.Add(Adr);
string Astr = DataTable2Xml(Atb);
string StrPath = AppDomain.CurrentDomain.BaseDirectory + "\\DBConnect.xml";
File.WriteAllText(StrPath, Astr, Encoding.GetEncoding("gb2312"));//不存在該XML檔案時會自動生成一個檔案
}
/// <summary>
/// 將XML生成DataTable
/// </summary>
/// <param name="xmlStr">XML字串</param>
/// <returns></returns>
public static DataTable XmlToDataTable(string xmlStr)
{
if (!string.IsNullOrEmpty(xmlStr))
{
StringReader StrStream = null;
XmlTextReader Xmlrdr = null;
try
{
DataSet ds = new DataSet();
//讀取字串中的資訊
StrStream = new StringReader(xmlStr);
//獲取StrStream中的資料
Xmlrdr = new XmlTextReader(StrStream);
//ds獲取Xmlrdr中的資料
ds.ReadXml(Xmlrdr);
return ds.Tables[0];
}
catch (Exception e)
{
return null;
}
finally
{
//釋放資源
if (Xmlrdr != null)
{
Xmlrdr.Close();
StrStream.Close();
StrStream.Dispose();
}
}
}
return null;
}
/// <summary>
/// 將datatable轉為xml
/// </summary>
/// <param name="vTable">要生成XML的DataTable</param>
/// <returns></returns>
public static string DataTable2Xml(DataTable vTable)
{
if (null == vTable) return string.Empty;
StringWriter writer = new StringWriter();
vTable.WriteXml(writer);
string xmlstr = writer.ToString();
writer.Close();
return xmlstr;
}
/// 讀取XML中的資料
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string StrPath = AppDomain.CurrentDomain.BaseDirectory + "\\DBConnect.xml";
string Astr = File.ReadAllText(StrPath, Encoding.GetEncoding("gb2312"));
DataTable Atbl = XmlToDataTable(Astr);
}
/// <summary>
/// 將Data Table的資料寫入到XML中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
DataTable Atb = new DataTable("Stu");
Atb.Columns.Add("Name");
Atb.Columns.Add("Sex");
DataRow Adr = Atb.NewRow();
Adr["Name"] = "高曉宇";
Adr["Sex"] = "22";
Atb.Rows.Add(Adr);
Adr = Atb.NewRow();
Adr["Name"] = "高小牛";
Adr["Sex"] = "1";
Atb.Rows.Add(Adr);
string Astr = DataTable2Xml(Atb);
string StrPath = AppDomain.CurrentDomain.BaseDirectory + "\\DBConnect.xml";
File.WriteAllText(StrPath, Astr, Encoding.GetEncoding("gb2312"));//不存在該XML檔案時會自動生成一個檔案
}
/// <summary>
/// 將XML生成DataTable
/// </summary>
/// <param name="xmlStr">XML字串</param>
/// <returns></returns>
public static DataTable XmlToDataTable(string xmlStr)
{
if (!string.IsNullOrEmpty(xmlStr))
{
StringReader StrStream = null;
XmlTextReader Xmlrdr = null;
try
{
DataSet ds = new DataSet();
//讀取字串中的資訊
StrStream = new StringReader(xmlStr);
//獲取StrStream中的資料
Xmlrdr = new XmlTextReader(StrStream);
//ds獲取Xmlrdr中的資料
ds.ReadXml(Xmlrdr);
return ds.Tables[0];
}
catch (Exception e)
{
return null;
}
finally
{
//釋放資源
if (Xmlrdr != null)
{
Xmlrdr.Close();
StrStream.Close();
StrStream.Dispose();
}
}
}
return null;
}
/// <summary>
/// 將datatable轉為xml
/// </summary>
/// <param name="vTable">要生成XML的DataTable</param>
/// <returns></returns>
public static string DataTable2Xml(DataTable vTable)
{
if (null == vTable) return string.Empty;
StringWriter writer = new StringWriter();
vTable.WriteXml(writer);
string xmlstr = writer.ToString();
writer.Close();
return xmlstr;
}