XML IList TO DataSet TO DataTable 相互转换

 1       //遍历XML 获得 DataSet
//XmlTextReader
static void Main(string[] args) 2 { 3 string xmlData = @"D:\study\XMLtest\XMLtest\bin\Debug\bookstore.xml"; 4 DataSet t = ConvertXMLToDataSet(xmlData); 5 Console.WriteLine(t); 6 7 } 8 9 private static DataSet ConvertXMLToDataSet(string xmlFile) 10 { 11 StringReader stream = null; 12 XmlTextReader reader = null; 13 try 14 { 15 XmlDocument xmld = new XmlDocument(); 16 xmld.Load(xmlFile); 17 DataSet xmlDS = new DataSet(); 18 stream = new StringReader(xmld.InnerXml); 19 reader = new XmlTextReader(stream); 20 xmlDS.ReadXml(reader); 21 return xmlDS; 22 //DataSet xmlDS = new DataSet(); 23 //stream = new StringReader(xmlData); 24 //reader = new XmlTextReader(stream); 25 //xmlDS.ReadXml(reader, XmlReadMode.ReadSchema); 26 //return xmlDS; 27 } 28 catch (Exception ex) 29 { 30 string strTest = ex.Message; 31 return null; 32 } 33 finally 34 { 35 if (reader != null) 36 reader.Close(); 37 } 38 }

 经过调试

 

 如何把 DataSet 转换成 DataTable  *** 注意,我这里的xml文件节点相对混乱,这样读取出来在Dataset中 有三个DataTable 然后怎么把DataTable 怎么给分离开来!

 

XML :

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <books>

 3   <book genre="update知鸟" ISBN="2-3631-4">

 4     <title name="csTitle属性">

 5           <ti>属性值</ti>

 6           <ti>属性值</ti>

 7           <ti>

 8        

 9     <title>CS从入门到精通</title>

10     <author>izhiniao捷</author>

11     <price>58.3</price>

12 

13 </ti>

14     </title>

15     <author>izhiniao捷</author>

16     <price>58.3</price>

17   </book>

18   <book genre="知鸟" ISBN="2-3631-4">

19     <title>CS从入门到精通</title>

20     <author>izhiniao</author>

21     <price>58.3</price>

22   </book>

23 </books>
XML

 

  

 

DataSet 转换成 DataTable:

1           List<DataTable> lTb = new List<DataTable>();

2             for (int i = 0; i < tttt.Tables.Count; i++)//主要就是这里的循环  

3             {

4                 lTb.Add(tttt.Tables[i]);

5             }

 

 

IList<T> 转换成 DataSet

这里的类我改成了 自定义的EMInfo,没用范型

 1 /// <summary>

 2         ///  Ilist<T> 转换成 DataSet

 3         /// </summary>

 4         /// <param name="list"></param>

 5         /// <returns></returns>

 6         public static DataSet ConvertToDataSet(IList<EMInfo> list) 

 7         {

 8             if (list == null || list.Count <= 0)

 9             {

10                 return null;

11             }

12 

13             DataSet ds = new DataSet();

14             DataTable dt = new DataTable(typeof(EMInfo).Name);

15             DataColumn column;

16             DataRow row;

17 

18             System.Reflection.PropertyInfo[] myPropertyInfo = typeof(EMInfo).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

19 

20             foreach (EMInfo t in list)

21             {

22                 if (t == null)

23                 {

24                     continue;

25                 }

26 

27                 row = dt.NewRow();

28 

29                 for (int i = 0, j = myPropertyInfo.Length; i < j; i++)

30                 {

31                     System.Reflection.PropertyInfo pi = myPropertyInfo[i];

32 

33                     string name = pi.Name;

34 

35                     if (dt.Columns[name] == null)

36                     {

37                         column = new DataColumn(name, typeof(String));

38                         dt.Columns.Add(column);

39                     }

40 

41                     row[name] = pi.GetValue(t, null);

42                 }

43 

44                 dt.Rows.Add(row);

45             }

46 

47             ds.Tables.Add(dt);

48 

49             return ds;

50         } 

 

你可能感兴趣的:(Datatable)