C#实现实体类和XML相互转换

港控/mmm° 2022-04-24 05:06 650阅读 0赞

C#实现实体类和XML相互转换

一、实体类转换成XML

将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化

  1. public static string XmlSerialize<T>(T obj)
  2. {
  3. using (StringWriter sw = new StringWriter())
  4. {
  5. Type t= obj.GetType();
  6. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  7. serializer.Serialize(sw, obj);
  8. sw.Close();
  9. return sw.ToString();
  10. }
  11. }

示例:

1、定义实体类

  1. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  2. [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  3. public class Request
  4. {
  5. public string System { get; set; }
  6. public string SecurityCode { get; set; }
  7. public PatientBasicInfo PatientInfo { get; set; }
  8. }
  9. /// <remarks/>
  10. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  11. public partial class PatientBasicInfo
  12. {
  13. public string PatientNo { get; set; }
  14. public string PatientName { get; set; }
  15. public string Phoneticize { get; set; }
  16. public string Sex { get; set; }
  17. public string Birth { get; set; }
  18. public string BirthPlace { get; set; }
  19. public string Country { get; set; }
  20. public string Nation { get; set; }
  21. public string IDNumber { get; set; }
  22. public string SecurityNo { get; set; }
  23. public string Workunits { get; set; }
  24. public string Address { get; set; }
  25. public string ZIPCode { get; set; }
  26. public string Phone { get; set; }
  27. public string ContactPerson { get; set; }
  28. public string ContactShip { get; set; }
  29. public string ContactPersonAdd { get; set; }
  30. public string ContactPersonPhone { get; set; }
  31. public string OperationCode { get; set; }
  32. public string OperationName { get; set; }
  33. public string OperationTime { get; set; }
  34. public string CardNo { get; set; }
  35. public string ChangeType { get; set; }
  36. }

2、给实体类赋值,并通过序列化将实体类转换成XML格式的字符串

  1. Request patientIn = new Request();
  2. patientIn.System = "HIS";
  3. patientIn.SecurityCode = "HIS5";
  4. PatientBasicInfo basicInfo = new PatientBasicInfo();
  5. basicInfo.PatientNo = "1234";
  6. basicInfo.PatientName = "测试";
  7. basicInfo.Phoneticize = "";
  8. basicInfo.Sex = "1";
  9. basicInfo.Birth = "";
  10. basicInfo.BirthPlace = "";
  11. basicInfo.Country = "";
  12. basicInfo.Nation = "";
  13. basicInfo.IDNumber = "";
  14. basicInfo.SecurityNo = "";
  15. basicInfo.Workunits = "";
  16. basicInfo.Address = "";
  17. basicInfo.ZIPCode = "";
  18. basicInfo.Phone = "";
  19. basicInfo.ContactShip = "";
  20. basicInfo.ContactPersonPhone = "";
  21. basicInfo.ContactPersonAdd = "";
  22. basicInfo.ContactPerson = "";
  23. basicInfo.ChangeType = "";
  24. basicInfo.CardNo = "";
  25. basicInfo.OperationCode = "";
  26. basicInfo.OperationName = "";
  27. basicInfo.OperationTime = "";
  28. patientIn.PatientInfo = basicInfo;
  29. //序列化
  30. string strxml = XmlSerializeHelper.XmlSerialize<Request>(patientIn);

3、生成的XML实例

  1. <?xml version="1.0" encoding="utf-16"?>
  2. <Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3. <System>HIS</System>
  4. <SecurityCode>HIS5</SecurityCode>
  5. <PatientInfo>
  6. <PatientNo>1234</PatientNo>
  7. <PatientName>测试</PatientName>
  8. <Phoneticize />
  9. <Sex>1</Sex>
  10. <Birth />
  11. <BirthPlace />
  12. <Country />
  13. <Nation />
  14. <IDNumber />
  15. <SecurityNo />
  16. <Workunits />
  17. <Address />
  18. <ZIPCode />
  19. <Phone />
  20. <ContactPerson />
  21. <ContactShip />
  22. <ContactPersonAdd />
  23. <ContactPersonPhone />
  24. <OperationCode />
  25. <OperationName />
  26. <OperationTime />
  27. <CardNo />
  28. <ChangeType />
  29. </PatientInfo>
  30. </Request>

二、将XML转换成实体类

把XML转换成相应的实体类,需要使用到XmlSerializer类的Deserialize方法,将XML进行反序列化。

  1. public static T DESerializer<T>(string strXML) where T:class
  2. {
  3. try
  4. {
  5. using (StringReader sr = new StringReader(strXML))
  6. {
  7. XmlSerializer serializer = new XmlSerializer(typeof(T));
  8. return serializer.Deserialize(sr) as T;
  9. }
  10. }
  11. catch (Exception ex)
  12. {
  13. return null;
  14. }
  15. }

示例:

将上例中序列化后的XML反序列化成实体类

//反序列化
Request r = XmlSerializeHelper.DESerializer(strxml);

三、将DataTable转换成XML
复制代码

//将DataTable转换成XML
DataTable dt = new DataTable(“MyTable”);
//添加列
dt.Columns.Add(“Id”, typeof(int));
dt.Columns.Add(“Name”, typeof(string));
dt.Columns.Add(“Sex”, typeof(char));
//添加行
dt.Rows.Add(1, “小明”, ‘1’);
dt.Rows.Add(2, “小红”, ‘2’);
dt.Rows.Add(3, “小王”, ‘2’);
dt.Rows.Add(4, “测试”, ‘2’);
//序列化,将DataTable转换成XML格式的字符串
string strXML = XmlSerializeHelper.XmlSerialize (dt);

复制代码

四、将XML转换成DataTable

1 //反序列化,将XML转换成字符串
2 DataTable dtNew= XmlSerializeHelper.DESerializer(strXML);

五、将List集合转换成XML
复制代码

///


/// 测试类
///

public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public char Sex { get; set; }
public int Age { get; set; }
}

//测试集合
List list = new List()
{
new Student(){Id=1,Name=”小红”,Sex=’2’,Age=20},
new Student(){Id=2,Name=”小明”,Sex=’1’,Age=22},
new Student(){Id=3,Name=”小王”,Sex=’1’,Age=19},
new Student(){Id=4,Name=”测试”,Sex=’2’,Age=23}
};
//序列化
string strXML = XmlSerializeHelper.XmlSerialize>(list);

复制代码

六、将XML转换成集合

使用上面例子中集合转换成的XML进行反序列化。

//反序列化
List listStu = XmlSerializeHelper.DESerializer>(strXML);

https://www.cnblogs.com/hnsongbiao/p/7717410.html

posted @ 2019-01-22 13:40 波霸38 阅读( …) 评论( …) 编辑 收藏

发表评论

表情:
评论列表 (有 0 条评论,650人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C#实体XML相互转换

    1、实体类与XML相互转换 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化。 把XML转换成相应的实体类,需要使用到Xml