DataSet与Xml之间的转换
DataSet轉化為Xml,Xml轉化為DataSet,Ado.net中使用方法DataSet.WriteXml()和DataSet.ReadXml(),由于有多個重載方法,所以這里有多條路徑可以實現,可謂條條大道通羅馬。這里我就把所有的用法來嘗試下,希望給大家有個借鑒作用。?????
因為DataSet.ReadXml()和DataSet.WriteXml()共8種重載方法,前四種是不帶架構的,后四種帶架構。這里舉出帶架構的4種方法。??????
/// <summary>???????? /// DataSet和Xml之間的轉換???????? /// </summary>???????? /// <param name="sender"></param>???????? /// <param name="e"></param>???????? private void DsAndXml_Click(object sender, EventArgs e)????????
{???????????? //自定義一個DataSet,里面有一列a,一行值為"hello world!",Xml數據從這個數據集中來讀取????????????
DataSet dsWrite = new DataSet();????????????
DataTable dt = new DataTable();????????????
?dt.Columns.Add("a", typeof(string));????????????
DataRow dr = dt.NewRow();????????????
dr[0] = "hello world!";????????????
?dt.Rows.Add(dr);????????????
dsWrite.Tables.Add(dt);????????????
//自定義一個DataSet用來從xml中讀取數據????????????
DataSet dsRead = new DataSet();????????????
?int k = Convert.ToInt32(this.textBox1.Text.ToString());???????
? switch(k)????????????
?{???????????case 1:
?Stream stream = new FileStream(@System.AppDomain.CurrentDomain.BaseDirectory.ToString()+"Test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);????????????????????
//dsWrite.WriteXml(stream);?????????????????
?dsWrite.WriteXml(stream, XmlWriteMode.WriteSchema);?????????????????
?stream.Close();?????????????????????
?dsRead.ReadXml(@System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "Test.txt");????????????????????
break;?
case 2:
dsWrite.WriteXml(@System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "Test.txt");???????????????????????
dsRead.ReadXml(@System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "Test.txt");????????????????????
break;?
case 3:?
StringBuilder s1 = new StringBuilder();????????????????????
TextWriter tw1 = new StringWriter(s1);????????????????????
?System.Xml.XmlTextWriter xtw1= new System.Xml.XmlTextWriter(tw1);????????????????????
dsWrite.WriteXml(xtw1,XmlWriteMode.WriteSchema);????????????????????
string xml1 = s1.ToString();???????????????????? //讀入????????????????????
TextReader tr1 = new StringReader(xml1);????????????????????
dsRead.ReadXml(tr1,XmlReadMode.ReadSchema);????????????????????
?//System.Xml.XmlReader xr = System.Xml.XmlReader.Create(tr1);????????????????????
//dsRead.ReadXml(xr);???????????????????????????????????????
break;????????????????
case 4:
StringBuilder s = new StringBuilder();????????????????????
?TextWriter tw = new StringWriter(s);????????????????????
dsWrite.WriteXml(tw, XmlWriteMode.WriteSchema);????????????????????
string xml = s.ToString();???????????????????
TextReader tr = new StringReader(xml);????????????????????
dsRead.ReadXml(tr,XmlReadMode.ReadSchema);????????????????????
?break;????????????????
?default:????????????????????
break;??????????????
}????????
}?????
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/feijian14/archive/2008/06/28/2593930.aspx
轉載于:https://www.cnblogs.com/uwking/archive/2011/04/13/2014584.html
總結
以上是生活随笔為你收集整理的DataSet与Xml之间的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工业设备数据采集系统-采集精灵
- 下一篇: php cookie 字串,php入门(