【插件开发】C# 中 JSON、DataTable、XML 和对象类的精彩转换与反序列化实战

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

【插件开发】C# 中 JSON、DataTable、XML 和对象类的精彩转换与反序列化实战

**一、对象类与JSON的转换** ``` using System; using Newtonsoft.Json; class Program { class Person { public string Name { get; set; } public int Age { get; set; } } static void Main() { // 对象转 JSON Person person = new Person { Name = "Alice", Age = 25 }; string json = JsonConvert.SerializeObject(person); Console.WriteLine(json); // JSON 转对象 string jsonData = "{\"Name\":\"Bob\",\"Age\":30}"; Person deserializedPerson = JsonConvert.DeserializeObject<Person>(jsonData); Console.WriteLine(deserializedPerson.Name + " " + deserializedPerson.Age); } } ``` **二、DataTable与JSON的转换** ``` using System; using System.Data; using Newtonsoft.Json; class Program { static void Main() { // 创建 DataTable DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Age", typeof(int)); table.Rows.Add("Alice", 25); table.Rows.Add("Bob", 30); // DataTable 转 JSON string json = JsonConvert.SerializeObject(table); Console.WriteLine(json); // JSON 转 DataTable string jsonData = "[{\"Name\":\"Charlie\",\"Age\":35},{\"Name\":\"David\",\"Age\":40}]"; DataTable deserializedTable = JsonConvert.DeserializeObject<DataTable>(jsonData); foreach (DataRow row in deserializedTable.Rows) { Console.WriteLine(row["Name"] + " " + row["Age"]); } } } ``` **三、XML与JSON的转换** ``` using System; using System.Xml; using Newtonsoft.Json; using Newtonsoft.Json.Linq; class Program { static void Main() { // JSON 转 XML string json = "{\"name\":\"Alice\",\"age\":25}"; XmlDocument xmlDoc = JsonToXml(json); Console.WriteLine(xmlDoc.OuterXml); // XML 转 JSON string xml = "<person><name>Bob</name><age>30</age></person>"; string jsonResult = XmlToJson(xml); Console.WriteLine(jsonResult); } static XmlDocument JsonToXml(string json) { JObject jsonObject = JObject.Parse(json); XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(root); foreach (var property in jsonObject.Properties()) { XmlElement element = xmlDoc.CreateElement(property.Name); element.InnerText = property.Value.ToString(); root.AppendChild(element); } return xmlDoc; } static string XmlToJson(string xml) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); JObject jsonObject = new JObject(); foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) { jsonObject.Add(node.Name, node.InnerText); } return jsonObject.ToString(); } } ``` **四、DataTable与对象类的转换** ``` using System; using System.Data; class DataTableObjectConversion { class Person { public string Name { get; set; } public int Age { get; set; } } // DataTable 转对象类 public static List<Person> DataTableToObject(DataTable dt) { List<Person> persons = new List<Person>(); foreach (DataRow row in dt.Rows) { Person person = new Person { Name = row["Name"].ToString(), Age = Convert.ToInt32(row["Age"]) }; persons.Add(person); } return persons; } // 对象类转 DataTable public static DataTable ObjectToDataTable(List<Person> persons) { DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Age", typeof(int)); foreach (var person in persons) { dt.Rows.Add(person.Name, person.Age); } return dt; } } ``` **五、DataTable与XML的转换** ``` using System; using System.Data; using System.Xml; class DataTableXmlConversion { // DataTable 转 XML public static XmlDocument DataTableToXml(DataTable dt) { XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement("Data"); xmlDoc.AppendChild(root); foreach (DataRow row in dt.Rows) { XmlElement dataElement = xmlDoc.CreateElement("Record"); foreach (DataColumn column in dt.Columns) { XmlElement columnElement = xmlDoc.CreateElement(column.ColumnName); columnElement.InnerText = row[column.ColumnName].ToString(); dataElement.AppendChild(columnElement); } root.AppendChild(dataElement); } return xmlDoc; } // XML 转 DataTable public static DataTable XmlToDataTable(string xml) { DataTable dt = new DataTable(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNodeList recordNodes = xmlDoc.SelectNodes("//Record"); foreach (XmlNode recordNode in recordNodes) { DataRow newRow = dt.NewRow(); foreach (XmlNode columnNode in recordNode.ChildNodes) { newRow[columnNode.Name] = columnNode.InnerText; } dt.Rows.Add(newRow); } return dt; } } ``` **六、XML与对象类的转换** ``` using System; using System.Xml.Serialization; class Program { [XmlRoot("Person")] class Person { [XmlElement("Name")] public string Name { get; set; } [XmlElement("Age")] public int Age { get; set; } } static void Main() { // 对象转 XML Person person = new Person { Name = "Alice", Age = 25 }; XmlSerializer serializer = new XmlSerializer(typeof(Person)); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, person); string xml = writer.ToString(); Console.WriteLine(xml); } // XML 转对象 string xmlData = "<person><name>Bob</name><age>30</age></person>"; using (StringReader reader = new StringReader(xmlData)) { Person deserializedPerson = (Person)serializer.Deserialize(reader); Console.WriteLine(deserializedPerson.Name + " " + deserializedPerson.Age); } } } ```

【插件开发】C# 中 JSON、DataTable、XML 和对象类的精彩转换与反序列化实战

**一、对象类与JSON的转换**```using System;using Newtonsoft.Json;class Program{ class Person { pub...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息