將數據導出到xml,並從xml導入數據

將數據導出到 xml ,並從 xml 導入數據
xml 本質上是數據,是另外一種格式的數據。而 ADO.NET 就是用來處理數據的。所以經過 DataSet 能夠很是方便的處理 xml 。下面這段程序對以下問題作出瞭解答:
生成的XML沒有表之間的關係怎麼辦?
怎樣把列數據做爲子元素?(在利用數據集獲得XML時)
怎樣把列數據做爲屬性?(在利用數據集獲得XML時)
怎樣利用數據集造成XML文件?
怎樣設置xml的namespace和prefix?
怎樣控制控制文本的格式?
怎樣把XML文件裏的數據讀入數據集?
怎樣利用XML文件傳遞數據集架構?
怎樣利用XML文件在不一樣數據集之間傳遞更改?
using System;
using System.Data;
using System.Data.OleDb;
using General.ADONET;
using General.ADONET.OLEDBClient;
using System.IO;
using System.Xml;
namespace PlayXML
{
///<summary>
/// PlayXML 的摘要說明。
///</summary>
public class XMLPlayer
{
public XMLPlayer()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
private Adapter adptEmp;
private Adapter adptDept;
private OleDbConnection conn;
private DataSet dsHR;
//初始化數據集
public voidInitDataSet()
{
string strConn = "Provider = Microsoft.Jet.OLEDB.4.0;";
strConn += "Data Source = E:\\everyday\\VS\\HELPREN\\hr.mdb;";
this.conn = new OleDbConnection(strConn);
dsHR = new DataSet();
this.adptEmp =
new Adapter(this.conn, dsHR, "employees");
this.adptDept =
new Adapter(this.conn, dsHR, "departments");
this.adptDept.Fill();
this.adptEmp.Fill();
DataTable dtDept = this.dsHR.Tables["departments"];
DataTable dtEmp = this.dsHR.Tables["employees"];
DataColumn colParent = dtDept.Columns["dept_id"];
DataColumn colChild = dtEmp.Columns["emp_department"];
dsHR.Relations.Add(
"departments_employees",
colParent, colChild);//關係
}
//怎樣從DataSet獲得xml?
public void GetXml()
{
Console.WriteLine(
this.dsHR.GetXml()//這個方法就是從DataSet獲得xml
);
}
//生成的XML沒有表之間的關係怎麼辦?
public void GetXmlWithRalation()
{
//指定DataRelation在DataSet中進行嵌套
//使xml序列化生成關係
this.dsHR.Relations["departments_employees"].Nested = true;
Console.WriteLine(
this.dsHR.GetXml()//這個方法就是從DataSet獲得xml
);
}
//怎樣把列數據做爲子元素?(在利用數據集獲得XML時)
public void GetSubElements()
{
DataTable dtEmp = this.dsHR.Tables["employees"];
foreach(DataColumn col in dtEmp.Columns)
{
//列的ColumnMapping屬性設置爲MappingType.Element就能夠了
col.ColumnMapping = MappingType.Element;
}
DataTable dtDept = this.dsHR.Tables["departments"];
foreach(DataColumn col in dtDept.Columns)
{
//列的ColumnMapping屬性設置爲MappingType.Element就能夠了
col.ColumnMapping = MappingType.Element;
}
this.dsHR.GetXml();
}
//怎樣把列數據做爲屬性?(在利用數據集獲得XML時)
public void GetAttributes()
{
DataTable dtEmp = this.dsHR.Tables["employees"];
foreach(DataColumn col in dtEmp.Columns)
{
//列的ColumnMapping屬性設置爲MappingType.Attribute就能夠了
col.ColumnMapping = MappingType.Attribute;
}
DataTable dtDept = this.dsHR.Tables["departments"];
foreach(DataColumn col in dtDept.Columns)
{
//列的ColumnMapping屬性設置爲MappingType.Attribute就能夠了
col.ColumnMapping = MappingType.Attribute;
}
this.dsHR.GetXml();
}
//怎樣利用數據集造成XML文件?
public void WriteXml()
{
//怎樣設置xml的namespace和prefix?
this.dsHR.Namespace = "ADONET";
this.dsHR.Prefix = "an";
this.dsHR.WriteXml("test.xml", XmlWriteMode.WriteSchema);
//怎樣控制控制文本的格式?
StreamWriter sw = new StreamWriter("test2.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 10;
this.dsHR.WriteXml(xmlWriter);
xmlWriter.Close();
}
//怎樣把XML文件裏的數據讀入數據集?
public void ReadXml()
{
DataSet dsNew = new DataSet();
dsNew.ReadXml("test.xml");
Console.WriteLine(dsNew.GetXml());
}
//怎樣利用XML文件傳遞數據集架構?
public void TransmitSchema()
{
StreamWriter sw = new StreamWriter("test.xsd");
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
this.dsHR.WriteXmlSchema(xmlWriter);
xmlWriter.Close();
DataSet dsNew = new DataSet();
dsNew.ReadXmlSchema("test.xsd");
foreach(DataTable dt in dsNew.Tables)
{
Console.WriteLine(dt.TableName);
foreach(DataColumn dc in dt.Columns)
{
Console.WriteLine(" " + dc.ColumnName);
}
}
}
//怎樣利用XML文件在不一樣數據集之間傳遞更改?
public void PlayDiffGram()
{
//爲兩個datatable設置主鍵
DataTable dtEmp = this.dsHR.Tables["employees"];
DataTable dtDept = this.dsHR.Tables["departments"];
dtEmp.PrimaryKey = new DataColumn[]
{dtEmp.Columns["emp_id"]};
dtDept.PrimaryKey = new DataColumn[]
{dtDept.Columns["dept_id"]};
//把數據集寫到xml文件
StreamWriter sw = new StreamWriter("test.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
this.dsHR.WriteXml(xmlWriter, XmlWriteMode.WriteSchema);
sw.Close();
//從xml文件讀入數據到一個新的數據集裏
DataSet dsNew = new DataSet();
dsNew.ReadXml("test.xml", XmlReadMode.ReadSchema);
dsNew.AcceptChanges();
//修改值
this.dsHR.Tables["employees"].Rows[0]["emp_name"] = "aaaaa";
dsNew.Tables["employees"].Rows[0]["emp_name"] = "ccccc";
//把新數據集裏的數據輸出到xml文件
Stream newStream = new MemoryStream() as Stream;
XmlTextWriter xmlNewWriter =
new XmlTextWriter(newStream, null);
xmlNewWriter.Formatting = Formatting.Indented;
dsNew.GetChanges().WriteXml(xmlNewWriter, XmlWriteMode.DiffGram);
//顯示如今xml文件的內容
newStream.Position = 0;
StreamReader rdr = new StreamReader(newStream);
Console.WriteLine(rdr.ReadToEnd());
newStream.Position = 0;
//把如今的xml文件的內容讀到原先的那個數據集
this.dsHR.ReadXml(newStream, XmlReadMode.DiffGram);
}
}
}


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=629963架構