DataTable / DataSet 与 xml 的相互转换

之前做DataTable和DataSet转xml一直使用XmlSerializer 序列化完成。今天发现新方法,哇咔咔方便了很多。还不用担心Name为空时报错

 1 static void Main(string[] args)
 2         {
 3             DataTable dt = new DataTable();
 4             dt.TableName = "dtABC";
 5             dt.Columns.Add("CloumnA");
 6             dt.Columns.Add("CloumnB");
 7             dt.Columns.Add("CloumnC");
 8
 9             DataRow dr = dt.NewRow();
10             dr["CloumnA"] = "AAA";
11             dr["CloumnB"] = "BBB";
12             dr["CloumnC"] = "AAA";
13
14             dt.Rows.Add(dr);
15             DataSet ds = new DataSet();
16             ds.DataSetName = "dsABC";
17             ds.Tables.Add(dt);
18
19             string xml = ds.GetXml();  //DataSet序列化成xml
20             //string xml2 = ds.GetXmlSchema();
21
22             DataSet ds2 = new DataSet("dsNEW");
23
24             StringReader stream = new StringReader(xml);  //xml反序列化成DataSet
25             XmlTextReader reader = new XmlTextReader(stream);
26             ds2.ReadXml(reader);
27         }
时间: 2024-10-10 22:29:03

DataTable / DataSet 与 xml 的相互转换的相关文章

DataSet - DataTable - DataRow 读取 xml 文件 + 搜索

DataSet ds = XmlHelper.GetDataSetByXml(AppDomain.CurrentDomain.BaseDirectory + "/Config/ConfigName.xml");//取得xml文件 DataTable dt = ds.Tables["table_Name"];//取得xml文件的节点内容 DataRow[] dr = dr = ds.Tables["table_Name"].Select("

DataSet与Xml文件的互相转换

DataSet转换为xml文件   //将DataSet转换为xml文件        private static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)        {            MemoryStream stream = null;            XmlTextWriter writer = null;                        try            {    

WebService传递XML数据 C#DataSet操作XML 解析WebService返回的XML数据

Webservice传递的数据只能是序列化的数据,典型的就是xml数据.   /// <summary>         /// 通过用户名和密码 返回下行数据         /// </summary>         /// <param name="UserName">用户名</param>         /// <param name="UserPwd">密码</param>    

c#解析XML到DATASET及dataset转为xml文件函数

//将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 = n

将DataTable转换成xml字符串

//将DataTable转换成xml字符串: public string ConvertDataTableToXml(DataTable dt) { MemoryStream stream = null; XmlTextWriter writer = null; try { stream = new MemoryStream(); writer = new XmlTextWriter(stream, System.Text.Encoding.Unicode); dt.WriteXml(write

C# 实现DataTable、DataSet与XML互相转换

/**//// <summary> /// 把DataSet.DataTable.DataView格式转换成XML字符串.XML文件 /// </summary> public class DataToXml { /**//// <summary> /// 将DataTable对象转换成XML字符串 /// </summary> /// <param name="dt">DataTable对象</param> //

dataset对XML的操作。writexml() and readxml。dataset.AcceptChanges()。dataset.DIspose()。释放资源

private void DemonstrateReadWriteXMLDocumentWithStreamReader() { // Create a DataSet with one table and two columns. DataSet OriginalDataSet = new DataSet("dataSet"); OriginalDataSet.Namespace= "NetFrameWork"; DataTable table = new Dat

DataSet 读取xml 报错有非法字符

private void Bind() { string strLogPath = ConfigurationSettings.AppSettings["LOG_PATH"].ToString(); if (strLogPath.Length > 0 && strLogPath[strLogPath.Length - 1] != '\\') strLogPath += '\\'; string strXMLUrl = String.Format("{0}

dataTable/dataSet转换成Json格式

using System.Text;using System.Collections.Generic; public static string DataTable2Json(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{\""); jsonBuilder.Append(dt.TableName.ToString()); jsonBuilder.App