XML与DataTable相互转换
1.DataTable转XML
#region DataTableToXml
/// <summary>
/// 将DataTable对象转换成XML字符串
/// </summary>
/// <param name="ds">DataSet对象</param>
/// <returns>XML字符串</returns>
public static string DataTableToXml(DataTable dt,string sName)
{
if (dt != null)
{
MemoryStream ms = null;
XmlTextWriter XmlWt = null;
try
{
ms = new MemoryStream();
//根据ms实例化XmlWt
XmlWt = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
//获取ds中的数据
dt.TableName = Sql.IsEmptyString(sName) ? "dt2xml" : sName;
dt.WriteXml(XmlWt, XmlWriteMode.WriteSchema);
int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
//返回Unicode编码的文本
System.Text.UnicodeEncoding ucode = new System.Text.UnicodeEncoding();
string returnValue = ucode.GetString(temp).Trim();
return returnValue;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//释放资源
if (XmlWt != null)
{
XmlWt.Close();
ms.Close();
ms.Dispose();
}
}
}
else
{
return "";
}
}
#endregion
2.XML转DataSet
#region Xml To DataSet
public static DataSet XmlToDataSet(string xmlString)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlString);
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmldoc.InnerXml);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
reader.Close();
return xmlDS;
}
catch (System.Exception ex)
{
reader.Close();
throw ex;
}
}
#endregion
XML与DataTable相互转换的更多相关文章
- XML与DataSet相互转换,DataSet查询
以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...
- Xml与DataTable相互转换方法
1.Xml与DataTable相互转换方法:http://www.cnblogs.com/lilin/archive/2010/04/18/1714927.html
- Springboot中使用Xstream进行XML与Bean 相互转换
在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...
- XML IList<T> TO DataSet TO DataTable 相互转换
//遍历XML 获得 DataSet //XmlTextReader static void Main(string[] args) { string xmlData = @"D:\stud ...
- 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参
转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...
- Sql Server参数化查询之where in和like实现之xml和DataTable传参 (转)
在上一篇Sql Server参数化查询之where in和like实现详解中介绍了在Sql Server使用参数化查询where in的几种实现方案,遗漏了xml和表值参数,这里做一个补充 文章导读 ...
- xml与datatable类型互换
//已测 private DataTable ConvertXMLToDataSet(string xmlData) { StringReader stream = null; XmlTextRead ...
- C#实现XML与DataTable互转
private string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; XmlTextWriter wr ...
- SQL 读取XML到Datatable
DECLARE @hdoc INT --XML 数据格式 --------------------------------------------------------- ) SET @doc = ...
随机推荐
- linux生成多对秘钥并指定秘钥登录
1.生成秘钥对并指定秘钥对名称: ssh-keygen -t rsa -f other -C "for other" 2.将other.pub公钥添加至要免密登录的服务器 3.使用 ...
- Android studio移动项目到另外一个文件夹,结果不能安装的解决方法
删除.gradle , .idea , build 三个文件夹即可
- PlayJava Day002
今日所学: /* 2019.08.19开始学习,此为补档. */ 流程控制 条件: 一重用if 二重用if ... else 三重用if ... else if ... else 多重用switch ...
- AppBoxFuture(八): 另类的ORM实现
通常的ORM实现基于配置或注释,由反射或Emit生成相应的Sql语句,然后将Sql发送给数据库解析Sql字符串生成AST再交给优化器处理后执行,返回的数据再经由反射或Emit转换为相应的实体实例. ...
- FCC---Use CSS Animation to Change the Hover State of a Button---鼠标移过,背景色变色,用0.5s的动画制作
You can use CSS @keyframes to change the color of a button in its hover state. Here's an example of ...
- 字体图标引入到HTML---复制用代码
第一步:在样式里面声明字体: 告诉别人我们自己定义的字体 ps: font-family的名字可以自己起 src是引入了4种字体,不用修改的 @font-face { font-family: 'ic ...
- 0基础入门学习Python(第3章)
第三章 成为高手前必须知道的一些基础知识 3.1 变量 一个值的名字,存储在内存中,我把把这块内存称为变量,大多数语言,把这个过程称之为,给变量赋值,把值存储在变量中. Python这里的做法稍有不同 ...
- [20191003]oracle number类型存储转化脚本.txt
[20191003]oracle number类型存储转化脚本.txt --//完善脚本,增加支持科学记数法.比如1e4之类的写法. 2.测试:$ cat test.txt012251234100-4 ...
- 元素无法定位问题 NoSuchElementException: Message: no such element: Unable to locate element 解决方法
定位网页上某个按钮时,总是报错元素定位不到,具体如下:NoSuchElementException: Message: no such element: Unable to locate elemen ...
- 如何使用DOM4j+xpath 解析
1.首先去dom4j官网下载一个对应版本的JAR包 XML的配置文件 导入一个config.xml文件 下面是dom4j+xpath解析XML文件