zoukankan      html  css  js  c++  java
  • c#大圣之路笔记——c# 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改。

    
    
      1   1   
      2   2 using System;
      3   3 using System.Collections.Generic;
      4   4 using System.Linq;
      5   5 using System.Text;
      6   6 using System.IO;
      7   7 using System.Data;
      8   8 using System.Xml;
      9   9 using System.Xml.Serialization;
     10  10  
     11  11 /// <summary>
     12  12 /// Xml序列化与反序列化
     13  13 /// </summary>
     14  14 public class XmlUtil
     15  15 {
     16  16     #region 反序列化
     17  17     /// <summary>
     18  18     /// 反序列化
     19  19     /// </summary>
     20  20     /// <param name="type">类型</param>
     21  21     /// <param name="xml">XML字符串</param>
     22  22     /// <returns></returns>
     23  23     public static object Deserialize(Type type, string xml)
     24  24     {
     25  25         try
     26  26         {
     27  27             using (StringReader sr = new StringReader(xml))
     28  28             {
     29  29                 XmlSerializer xmldes = new XmlSerializer(type);
     30  30                 return xmldes.Deserialize(sr);
     31  31             }
     32  32         }
     33  33         catch (Exception e)
     34  34         {
     35  35  
     36  36             return null;
     37  37         }
     38  38     }
     39  39     /// <summary>
     40  40     /// 反序列化
     41  41     /// </summary>
     42  42     /// <param name="type"></param>
     43  43     /// <param name="xml"></param>
     44  44     /// <returns></returns>
     45  45     public static object Deserialize(Type type, Stream stream)
     46  46     {
     47  47         XmlSerializer xmldes = new XmlSerializer(type);
     48  48         return xmldes.Deserialize(stream);
     49  49     }
     50  50     #endregion
     51  51  
     52  52     #region 序列化
     53  53     /// <summary>
     54  54     /// 序列化
     55  55     /// </summary>
     56  56     /// <param name="type">类型</param>
     57  57     /// <param name="obj">对象</param>
     58  58     /// <returns></returns>
     59  59     public static string Serializer(Type type, object obj)
     60  60     {
     61  61         MemoryStream Stream = new MemoryStream();
     62  62         XmlSerializer xml = new XmlSerializer(type);
     63  63         try
     64  64         {
     65  65             //序列化对象
     66  66             xml.Serialize(Stream, obj);
     67  67         }
     68  68         catch (InvalidOperationException)
     69  69         {
     70  70             throw;
     71  71         }
     72  72         Stream.Position = 0;
     73  73         StreamReader sr = new StreamReader(Stream);
     74  74         string str = sr.ReadToEnd();
     75  75          
     76  76         sr.Dispose();
     77  77         Stream.Dispose();
     78  78  
     79  79         return str;
     80  80     }
     81  81  
     82  82     #endregion
     83  83 }
     84  84 下面是测试代码:
     85  85  
     86  86 1. 实体对象转换到Xml
     87  87 
     88  97 public class Student
     89  98 {
     90  99     public string Name { set; get; }
     91 100     public int Age { set; get; }
     92 101 }
     93 102  
     94 103 Student stu1 = new Student() { Name = "okbase", Age = 10 };
     95 104 string xml = XmlUtil.Serializer(typeof(Student), stu1);
     96 105 Console.Write(xml);
     97 106 2. Xml转换到实体对象
     98 107   
     99 110 Student stu2 = XmlUtil.Deserialize(typeof(Student), xml) as Student;
    100 111 Console.Write(string.Format("名字:{0},年龄:{1}", stu2.Name, stu2.Age));
    101 112 3. DataTable转换到Xml
    102 113  
    103 114 
    104 132 // 生成DataTable对象用于测试
    105 133 DataTable dt1 = new DataTable("mytable");   // 必须指明DataTable名称
    106 134  
    107 135 dt1.Columns.Add("Dosage", typeof(int));
    108 136 dt1.Columns.Add("Drug", typeof(string));
    109 137 dt1.Columns.Add("Patient", typeof(string));
    110 138 dt1.Columns.Add("Date", typeof(DateTime));
    111 139  
    112 140 // 添加行
    113 141 dt1.Rows.Add(25, "Indocin", "David", DateTime.Now);
    114 142 dt1.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    115 143 dt1.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    116 144 dt1.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    117 145 dt1.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    118 146  
    119 147 // 序列化
    120 148 xml = XmlUtil.Serializer(typeof(DataTable), dt1);
    121 149 Console.Write(xml);
    122 150 4. Xml转换到DataTable
    123 1 
    124 165 // 反序列化
    125 166 DataTable dt2 = XmlUtil.Deserialize(typeof(DataTable), xml) as DataTable;
    126 167  
    127 168 // 输出测试结果
    128 169 foreach (DataRow dr in dt2.Rows)
    129 170 {
    130 171     foreach (DataColumn col in dt2.Columns)
    131 172     {
    132 173         Console.Write(dr[col].ToString() + " ");
    133 174     }
    134 175  
    135 176     Console.Write("
    ");
    136 177 }
    137 178 5. List转换到Xml
    138 179  
    139  
    140 188 // 生成List对象用于测试
    141 189 List<Student> list1 = new List<Student>(3);
    142 190  
    143 191 list1.Add(new Student() { Name = "okbase", Age = 10 });
    144 192 list1.Add(new Student() { Name = "csdn", Age = 15 });
    145 193 // 序列化
    146 194 xml = XmlUtil.Serializer(typeof(List<Student>), list1);
    147 195 Console.Write(xml);
    148 196 6. Xml转换到List
    149 19 
    150 203 List<Student> list2 = XmlUtil.Deserialize(typeof(List<Student>), xml) as List<Student>;
    151 204 foreach (Student stu in list2)
    152 205 {
    153 206     Console.WriteLine(stu.Name + "," + stu.Age.ToString());
    154 207 }
    155 208 从代码可以看到,千变万化不离其宗!
    
    
    
    
    
  • 相关阅读:
    7.25
    7.24
    7.23
    7.22
    输入语句/条件运算符
    flowLayoutPanel1设置内容随着鼠标滚动而滚动
    dataGridView读取xml文件
    读文本内容 写入文本内容 创建复制文本
    cmd.ExecuteScalar 和配置连接设置
    $.ajax async同步加载
  • 原文地址:https://www.cnblogs.com/allenzhang/p/5888575.html
Copyright © 2011-2022 走看看