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