之前做DataTable和DataSet转xml一直使用XmlSerializer 序列化完成。今天发现新方法,哇咔咔方便了很多。还不用担心Name为空时报错
1 static void Main(string[] args) 2 { 3 DataTable dt = new DataTable(); 4 dt.TableName = "dtABC"; 5 dt.Columns.Add("CloumnA"); 6 dt.Columns.Add("CloumnB"); 7 dt.Columns.Add("CloumnC"); 8 9 DataRow dr = dt.NewRow(); 10 dr["CloumnA"] = "AAA"; 11 dr["CloumnB"] = "BBB"; 12 dr["CloumnC"] = "AAA"; 13 14 dt.Rows.Add(dr); 15 DataSet ds = new DataSet(); 16 ds.DataSetName = "dsABC"; 17 ds.Tables.Add(dt); 18 19 string xml = ds.GetXml(); //DataSet序列化成xml 20 //string xml2 = ds.GetXmlSchema(); 21 22 DataSet ds2 = new DataSet("dsNEW"); 23 24 StringReader stream = new StringReader(xml); //xml反序列化成DataSet 25 XmlTextReader reader = new XmlTextReader(stream); 26 ds2.ReadXml(reader); 27 }
时间: 2024-10-10 22:29:03