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 = ...
随机推荐
- swoole的process模块创建和使用子进程
swoole中为我们提供了一个进程管理模块 Process,替换PHP的 pcntl 扩展,方便我们创建进程,管理进程,和进程间的通信. swoole提供了2种进程间的通信: 1.基于 unix so ...
- Netty — 线程模型
一.前言 众所周知,netty是高性能的原因源于其使用的是NIO,但是这只是其中一方面原因,其IO模型上决定的.另一方面源于其线程模型的设计,良好的线程模型设计,能够减少线程上下文切换,减少甚至避免锁 ...
- C#中对文件File常用操作方法的工具类
场景 C#中File类的常用读取与写入文件方法的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983 注: 博客 ...
- Add a Class from the Business Class Library 从业务类库添加类 (XPO)
In this lesson, you will learn how to use business classes from the Business Class Library as is. Fo ...
- MySQL Linux环境的安装配置
在Kali中已经内置了MySQL(镜像可以从mysql.com/downloads/ 下载安装) 奇怪的是博主我的kali内置的是mariaDB数据库,所以我也懒得弄MySQL了!直接mariaDB吧 ...
- NSURLSession的文件下载
小文件的下载,代码示例: //NSURLSession的下载 [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@ ...
- MySQL通过SHOW TABLE STATUS查看库中所有表的具体信息
有时候我们想看下指定库下所有表的使用情况,比如,查询表的Table大小,什么时候创建的,数据最近被更新的时间(即最近一笔insert/update/delete的时间).这些信息对我们进行库表维护很有 ...
- [Linux] deepin系统添加PHP仓库源出错Error: could not find a distribution template for Deepin/stable
aptsources.distro.NoDistroTemplateException: Error: could not find a distribution template for Deepi ...
- java链接集合
Intellij IDEA 导入eclipse web 项目详细操作 https://blog.csdn.net/deng11408205/article/details/79723213?utm_s ...
- 18.Llinux-触摸屏驱动(详解)【转】
转自:https://www.cnblogs.com/lifexy/p/7628889.html 本节的触摸屏驱动也是使用之前的输入子系统 1.先来回忆之前第12节分析的输入子系统 其中输入子系统层次 ...