首页 > 编程语言 >C#的DataTable、List、Dictionary、JSON、XML之间的转换

C#的DataTable、List、Dictionary、JSON、XML之间的转换

时间:2024-02-08 21:00:21浏览次数:28  
标签:XML Dictionary C# List JSON new DataTable

1. DataTable转List

// 创建一个DataTable对象
DataTable dataTable = new DataTable();

// 将DataTable转换为List<DataRow>
List<DataRow> dataList = dataTable.AsEnumerable().ToList();

 

2、DataTable转Dictionary

// 创建一个DataTable对象
DataTable dataTable = new DataTable();

// 将DataTable转换为Dictionary
Dictionary<string, object> dictionary = dataTable.AsEnumerable()
    .ToDictionary(row => row.Field<string>("KeyColumn"), row => row.Field<object>("ValueColumn"));

 

3. DataTable转JSON

// 创建一个DataTable对象
DataTable dataTable = new DataTable();

// 将DataTable转换为JSON字符串
string json = JsonConvert.SerializeObject(dataTable, Formatting.Indented);

 

4. DataTable转XML

// 创建一个DataTable对象
DataTable dataTable = new DataTable();

// 将DataTable转换为XML字符串
string xml = dataTable.GetXml();

 

5. List转DataTable

// 创建一个List<DataRow>对象
List<DataRow> dataList = new List<DataRow>();

// 将List<DataRow>转换为DataTable
DataTable dataTable = dataList.CopyToDataTable();

 

6. List转Dictionary

// 创建一个List<KeyValuePair<string, string>>对象
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

// 将List<KeyValuePair<string, string>>转换为Dictionary
Dictionary<string, string> dictionary = list.ToDictionary(x => x.Key, x => x.Value);

 

7. List转JSON

// 创建一个List<object>对象
List<object> list = new List<object>();

// 将List<object>转换为JSON字符串
string json = JsonConvert.SerializeObject(list, Formatting.Indented);

 

8. List转XML

// 创建一个List<Person>对象
List<Person> list = new List<Person>();

// 将List<Person>转换为XML字符串
string xml;
using (StringWriter stringWriter = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
serializer.Serialize(stringWriter, list);
xml = stringWriter.ToString();
}

 

9. Dictionary转List

// 创建一个Dictionary<string, string>对象
Dictionary<string, string> dictionary = new Dictionary<string, string>();

// 将Dictionary<string, string>转换为List<KeyValuePair<string, string>>
List<KeyValuePair<string, string>> list = dictionary.ToList();

 

10. Dictionary转DataTable

// 创建一个Dictionary<string, object>对象
Dictionary<string, object> dictionary = new Dictionary<string, object>();

// 创建一个DataTable对象,用于存储Dictionary的数据
DataTable dataTable = new DataTable();

// 将Dictionary的键作为列名,值作为数据行的值,添加到DataTable中
foreach (KeyValuePair<string, object> kvp in dictionary)
{
dataTable.Columns.Add(kvp.Key, kvp.Value.GetType());
}
dataTable.Rows.Add(dictionary.Values.ToArray());

 

11. Dictionary转JSON

// 创建一个Dictionary<string, object>对象
Dictionary<string, object> dictionary = new Dictionary<string, object>();

// 将Dictionary转换为JSON字符串
string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);

 

12. Dictionary转XML

// 创建一个Dictionary<string, object>对象
Dictionary<string, object> dictionary = new Dictionary<string, object>();

// 将Dictionary转换为XML字符串
string xml;
using (StringWriter stringWriter = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<string, object>));
serializer.Serialize(stringWriter, dictionary);
xml = stringWriter.ToString();
}

 

13. JSON转List

// 创建一个JSON字符串
string json = "[{\"name\":\"John\", \"age\":30}, {\"name\":\"Alice\", \"age\":25}]";

// 将JSON字符串转换为List<object>
List<object> list = JsonConvert.DeserializeObject<List<object>>(json);

 

14. JSON转Dictionary

// 创建一个JSON字符串
string json = "{\"name\":\"John\", \"age\":30}";

// 将JSON字符串转换为Dictionary<string, object>
Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

 

15. JSON转DataTable

// 创建一个JSON字符串
string json = "[{\"Name\":\"John\", \"Age\":30}, {\"Name\":\"Alice\", \"Age\":25}]";

// 将JSON字符串转换为DataTable
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(json);

 

16. JSON转XML

// 创建一个JSON字符串
string json = "{\"name\":\"John\", \"age\":30}";

// 将JSON字符串转换为XML字符串
XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(json);
string xml = xmlDoc.OuterXml;

 

17. XML转List

// 创建一个XML字符串
string xml = "<Persons><Person><Name>John</Name><Age>30</Age></Person><Person><Name>Alice</Name><Age>25</Age></Person></Persons>";

// 将XML字符串转换为List<Person>
List<Person> list;
using (StringReader stringReader = new StringReader(xml))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
list = (List<Person>)serializer.Deserialize(stringReader);

 

18. XML转Dictionary

// 创建一个XML字符串
string xml = "<Person><Name>John</Name><Age>30</Age></Person>";

// 将XML字符串转换为Dictionary<string, object>
Dictionary<string, object> dictionary;
using (StringReader stringReader = new StringReader(xml))
{
XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<string, object>));
dictionary = (Dictionary<string, object>)serializer.Deserialize(stringReader);
}

 

19. XML转DataTable

// 创建一个XML字符串
string xml = "<Persons><Person><Name>John</Name><Age>30</Age></Person><Person><Name>Alice</Name><Age>25</Age></Person></Persons>";

// 将XML字符串转换为DataTable
DataSet dataSet = new DataSet();
dataSet.ReadXml(new StringReader(xml));
DataTable dataTable = dataSet.Tables[0];

 

20. XML转JSON

// 创建一个XML字符串
string xml = "<Person><Name>John</Name><Age>30</Age></Person>";

// 将XML字符串转换为JSON字符串
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(xmlDoc);

 

标签:XML,Dictionary,C#,List,JSON,new,DataTable
From: https://www.cnblogs.com/epsoft/p/18012124

相关文章

  • [CSS] 字体渐变色
    实现思路:盒子设置为inline-block,这样盒子的大小就由文本量决定;盒子背景图片使用linear-gradient设置渐变色;设置background-clip:text;使用文字区域截取背景;此时文字覆盖在背景上方,只能看到原本纯色的文字,设置文本颜色为透明:color:transparent;,就能看到底部的渐变色的文字。......
  • C++OpenCV 按之前图片名称保存图片
    C++OpenCV按之前图片名称保存图片://图片地址cv::Stringpattern="./path/*.jpeg";//可以使用cv::glob函数来获取匹配模式的文件路径列表std::vector<cv::String>file_paths;cv::glob(pattern,file_paths);for(size_ti=0;i<file_paths.size();i++){//......
  • VS2022+OpenCV_contrib安装
    准备:Cmake,OpenCV安装包,OpenCV扩展包安装步骤:一:OpenCV扩展包编译打开文件夹新建一个文件夹 打开cmake开始编译第一栏Whereisthesourcecode是指OpenCV解压后得到的source文件的路径;第二栏wheretobuildthebinaries是指编译后输出文件的路径,直接在opencv的同个大文件......
  • c++ OpenCV 读取某个文件夹下的所有图片
    方案一:将文件夹中图像遍历到vector中#include<vector>#include<opencv2/imgproc.hpp>#include<opencv2/highgui.hpp>std::vector<cv::Mat>ReadImage(cv::Stringpattern){std::vector<cv::String>fn;cv::glob(pattern,fn,false);......
  • [CSS] 字体渐变色
    实现思路:盒子设置为inline-block,这样盒子的大小就由文本量决定;盒子背景图片使用linear-gradient设置渐变色;设置background-clip:text;使用文字区域截取背景;此时文字覆盖在背景上方,只能看到原本纯色的文字,设置文本颜色为透明:color:transparent;,就能看到底部的渐变色的文字。......
  • IfcRailingTypeEnum
    IfcRailingTypeEnum类型定义此枚举定义了可以使用枚举值预定义的IfcRailing或IfcRailingType的不同类型。 IFC2.0中的新枚举类型 EnumerationdefinitionConstantDescriptionHANDRAILAtypeofrailingdesignedtoserveasanoptionalstructuralsupportforlo......
  • openGauss学习笔记-216 openGauss性能调优-确定性能调优范围-硬件瓶颈点分析-CPU
    openGauss学习笔记-216openGauss性能调优-确定性能调优范围-硬件瓶颈点分析-CPU获取openGauss节点的CPU、内存、I/O和网络资源使用情况,确认这些资源是否已被充分利用,是否存在瓶颈点。216.1CPU通过top命令查看openGauss内节点CPU使用情况,分析是否存在由于CPU负载过高导致的性能......
  • CF1861E Non-Intersecting Subpermutations 题解
    简要题意一个长度为\(n\)的元素在\([1,k]\)的整数序列\(a\)的价值定义如下。初始\(i=1\),如果\(a_{i\simi+k-1}\)包含了\(1\simk\)的所有整数,则价值加\(1\),然后令\(i=i+k\)。如果没有包含\(1\simk\)的所有整数则\(i=i+1\),直到\(i\geqn-k+2\)时结束。......
  • CF1863F Divide, XOR, and Conquer 题解
    简要题意你有两个指针\(l,r\)初始时\(l=1,r=n\)。你可以操作若干次知道\(l=r\)。每次操作你可以选择一个整数\(k\in[l,r)\),记\(x=\bigoplus_{i=l}^ka_i,y=\bigoplus_{i=k+1}^ra_i\)。如果\(x\leqy\),你可以令\(l=k+1\)。如果\(x\geqy\),你可以令\(r=k\)。......
  • P9697 Canvas 题解
    首先观察到有一个关键性质是\(1\leqx_i,y_i\leq2\)。那么我们贪心的考虑我们肯定会将\((x,y)=(1,1)\)的在一开始操作,\((x,y)=(2,2)\)的最后操作。也就是说现在没有固定的是\((x,y)=(1,2)\)和\((x,y)=(2,1)\)的。我们不妨令\((x,y)=(1,2)\),然后连边\((l,r)\)。然......