//遍历XML 获得 DataSet
//XmlTextReader
static void Main(string[] args)
{
string xmlData = @"D:\study\XMLtest\XMLtest\bin\Debug\bookstore.xml";
DataSet t = ConvertXMLToDataSet(xmlData);
Console.WriteLine(t); } private static DataSet ConvertXMLToDataSet(string xmlFile)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlFile);
DataSet xmlDS = new DataSet();
stream = new StringReader(xmld.InnerXml);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
//DataSet xmlDS = new DataSet();
//stream = new StringReader(xmlData);
//reader = new XmlTextReader(stream);
//xmlDS.ReadXml(reader, XmlReadMode.ReadSchema);
//return xmlDS;
}
catch (Exception ex)
{
string strTest = ex.Message;
return null;
}
finally
{
if (reader != null)
reader.Close();
}
}

经过调试

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

XML :

 <?xml version="1.0" encoding="utf-8"?>
<books>
<book genre="update知鸟" ISBN="2-3631-4">
<title name="csTitle属性">
<ti>属性值</ti>
<ti>属性值</ti>
<ti> <title>CS从入门到精通</title>
<author>izhiniao捷</author>
<price>58.3</price> </ti>
</title>
<author>izhiniao捷</author>
<price>58.3</price>
</book>
<book genre="知鸟" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>izhiniao</author>
<price>58.3</price>
</book>
</books>

XML

DataSet 转换成 DataTable:

           List<DataTable> lTb = new List<DataTable>();
for (int i = ; i < tttt.Tables.Count; i++)//主要就是这里的循环
{
lTb.Add(tttt.Tables[i]);
}

IList<T> 转换成 DataSet

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

 /// <summary>
/// Ilist<T> 转换成 DataSet
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static DataSet ConvertToDataSet(IList<EMInfo> list)
{
if (list == null || list.Count <= )
{
return null;
} DataSet ds = new DataSet();
DataTable dt = new DataTable(typeof(EMInfo).Name);
DataColumn column;
DataRow row; System.Reflection.PropertyInfo[] myPropertyInfo = typeof(EMInfo).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (EMInfo t in list)
{
if (t == null)
{
continue;
} row = dt.NewRow(); for (int i = , j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i]; string name = pi.Name; if (dt.Columns[name] == null)
{
column = new DataColumn(name, typeof(String));
dt.Columns.Add(column);
} row[name] = pi.GetValue(t, null);
} dt.Rows.Add(row);
} ds.Tables.Add(dt); return ds;
}

XML IList<T> TO DataSet TO DataTable 相互转换的更多相关文章

  1. C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

    LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...

  2. Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader

    前文介绍了Winform为DataGridView提供的数据自动绑定功能,下面介绍一下采用代码的数据绑定 1.用DataSet和DataTable为DataGridView提供数据源 先上代码 pri ...

  3. DataSet与DataTable基本用法

    http://files.cnblogs.com/files/monkeyZhong/DataSetExample.rar 在设计数据库操作或者XML操作等表格数据时,我们难免要跟DataSet和Da ...

  4. 关于PagedDataSource分页属性与DataSet和DataTable详解

    Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...

  5. C# DataSet与DataTable的区别和用法

    DataSet是数据集,DataTable是数据表,DataSet存储多个DataTable.DataSet和DataTable像是专门存储数据的一个容器,在你查询数据库得到一些结果时可以存在里面. ...

  6. C# DataSet与DataTable的区别和用法 ---转载

    C# DataSet与DataTable的区别和用法 转载:https://www.cnblogs.com/liuyi-li/p/6340411.html DataSet是数据集,DataTable是 ...

  7. .NET DataSet、DataTable操作记录

    一直在习惯.net的编程思维,或是说C#吧.因为前几年一直在用PHP做站,现在用.net很不习惯,主要C#都依赖对控件.类的熟悉,不然很多功能都实现不了. 需求 最近做了一功能,从SQL Server ...

  8. C# DataSet和DataTable详解

    1.C# DataSet和DataTable详解:http://www.cnblogs.com/top5/archive/2009/04/23/1441765.html 2.DataSet和DataT ...

  9. 转:DataSet、DataTable、DataRow、DataColumn区别及使用实例

    DataSet 表示数据在内存中的缓存. 属性 Tables  获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据的 ...

随机推荐

  1. hive 分区操作记录

    创建分区: alter table table_name add partition (dt='20150423') location '/data/text/20150423';

  2. js表格排序 & 去除字符串空格

    // a:列数 bool:排序升序判断参数 true false Str:支持多列 function newUnitSort(a, bool, str) { var oTable = document ...

  3. 0520 python

    配置python环境变量我的电脑->右键->属性->高级系统设置->环境变量->(1)用户变量->新建 Path=C:\Python27(2)系统变量->编辑 ...

  4. poj 2653 计算几何

    #include <iostream> #include <cstring> #include <algorithm> #include <cmath> ...

  5. poj 2398 计算几何

    #include <iostream> #include<cstdio> #include<cstring> #include <algorithm> ...

  6. 图标字体IcoMoon 使用

    IcoMoon 使用官方地址 http://icomoon.io/实际上,它是一种字体,只不过这种字体的字象图标一样,比如windows中自带的MT Extra Webdings Wingdings字 ...

  7. LintCode-丢失的第一个正整数

    题目描述: 给出一个无序的正数数组,找出其中没有出现的最小正整数. 样例 如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2 挑战 只允许时间复杂度O(n ...

  8. nginx启动、开机自启动、重启、关闭

    yum -y  install nginx # yum info nginx Loaded plugins: fastestmirror Loading mirror speeds from cach ...

  9. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

  10. Modular Inverse(模逆元,扩展欧几里德)

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...