XML与DataSet相互转换,DataSet查询
以FileShare.Read形式读XML文件:
string hotspotXmlStr = string.Empty;
try
{
Stream fileStream = new FileStream(context.Server.MapPath("../data/Hotspot.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8);
hotspotXmlStr = streamReader.ReadToEnd();
streamReader.Dispose();
}
catch (Exception ex)
{
logger.Error("读取XML文件Hotspot.xml出现异常!");
logger.Error("异常描述:\t" + ex.Message);
logger.Error("引发异常的方法:\t" + ex.TargetSite);
logger.Error("异常堆栈:\t" + ex.StackTrace);
}
XML与DataSet相互转换的类(出处):
class XmlDatasetConvert
{
//将xml对象内容字符串转换为DataSet
public static DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
} //将xml文件转换为DataSet
public static DataSet ConvertXMLFileToDataSet(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);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
//xmlDS.ReadXml(xmlFile);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
} //将DataSet转换为xml对象字符串
public static string ConvertDataSetToXML(DataSet xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null; try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode); //用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(, SeekOrigin.Begin);
stream.Read(arr, , count); UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
} //将DataSet转换为xml文件
public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null; try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode); //用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(, SeekOrigin.Begin);
stream.Read(arr, , count); //返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
} }
DataSet查询,参考DataTable.Select 方法。
XML与DataSet相互转换,DataSet查询的更多相关文章
- Springboot中使用Xstream进行XML与Bean 相互转换
在现今的项目开发中,虽然数据的传输大部分都是用json格式来进行传输,但是xml毕竟也会有一些老的项目在进行使用,正常的老式方法是通过获取节点来进行一系列操作,个人感觉太过于复杂.繁琐.推荐一套简单的 ...
- Hibernate中:不看数据库,不看XML文件,不看查询语句,怎么样能知道表结构?
Hibernate中:不看数据库,不看XML文件,不看查询语句,怎么样能知道表结构? 解答:可以看与XML文件对应的域模型.
- from flyai.dataset import Dataset 报错
from flyai.dataset import Dataset 报错 No module name 'flyai' 先找到ide中使用的Python对应的pip的位置. windows用户 ...
- XML IList<T> TO DataSet TO DataTable 相互转换
//遍历XML 获得 DataSet //XmlTextReader static void Main(string[] args) { string xmlData = @"D:\stud ...
- c#解析XML到DATASET及dataset转为xml文件函数
//将xml对象内容字符串转换为DataSet public static DataSet ConvertXMLToDataSet(string xmlData) { ...
- ASP.NET中把xml转为dataset与xml字符串转为dataset及dataset转为xml的代码
转自:http://www.cnblogs.com/_zjl/archive/2011/04/08/2009087.html XmlDatasetConvert.csusing System;usin ...
- [C#]Winform后台提交数据且获取远程接口返回的XML数据,转换成DataSet
#region 接口返回的Xml转换成DataSet /// <summary> /// 返回的Xml转换成DataSet /// </summary> /// <par ...
- C#中Json和List/DataSet相互转换
#region List<T> 转 Json /// <summary> /// List<T> 转 Json /// & ...
- 泛型集合与DataSet相互转换
一.泛型转DataSet /// <summary> /// 泛型集合转换DataSet /// </summary> /// <typeparam name=" ...
随机推荐
- mysql,多表的内外连接+子查询
表: student house course 关系:student_course 多对多 student house 多对一 需求:查询房间1 的学生 都学习了什么课程 select s.s_nam ...
- ecshop 在首页每个商品下显示已销售数量
1.在includes/lib_goods.php文件末尾加入以下代码 function get_buy_sum($goods_id) { $sql = "select sum(goods_ ...
- Apache Conf/Vost/xx.conf 文件配置
#监听8086端口Listen 8086 NameVirtualHost *:8086#保持绝对一致 <VirtualHost *:> ServerName localhost Serve ...
- 加密app.config
EncryptConnection.EncryptConnectionString(true); public static class EncryptConnection { public stat ...
- MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)
MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...
- UITableViewCell 自定义绘制 性能高
// // FoodListTableViewCellB.m // BabyFood // // Created by zhuang chaoxiao on 16/3/7. // Copyri ...
- 我常用的VBS方法(QTP)
这些是4年前在HP用QTP做自动化测试时候总结的一些,现在贴出来,说不准以后会不会用到 当初花了2天时间写的一个自动生成的Excel Report Public Function Report (st ...
- 改进duilib的richedit控件的部分功能
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 如果要使用透明异形窗体功能,首先要改进duilib库让他本 ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- extern "c" 的作用
作用:实现C和C++混合编程. 原理:C和C++编译器编译之后,函数名会编译成不同的名字,链接阶段名字查找会找不到目标,后面实例中会详解. 用法:①.c文件中定义的函数,.cpp文件要调用时,该.cp ...