C#实现实体类和XML相互转换的示例代码详解

黄舟
Lepaskan: 2017-03-16 11:47:26
asal
1816 orang telah melayarinya

这篇文章主要为大家详细介绍了C#实现实体类和XML相互转换的资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、实体类转换成XML

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


public static string XmlSerialize<T>(T obj)
{
  using (StringWriter sw = new StringWriter())
  {
    Type t= obj.GetType();    
    XmlSerializer serializer = new XmlSerializer(obj.GetType());
    serializer.Serialize(sw, obj);
    sw.Close();
    return sw.ToString();
  }
}
Salin selepas log masuk

示例:

1、定义实体类


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
 [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
 public class Request
 {

  public string System { get; set; }
  public string SecurityCode { get; set; }
  public PatientBasicInfo PatientInfo { get; set; }  
 }

 /// <remarks/>
 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
 public partial class PatientBasicInfo
 {
  public string PatientNo { get; set; }
  public string PatientName { get; set; }
  public string Phoneticize { get; set; }
  public string Sex { get; set; }
  public string Birth { get; set; }
  public string BirthPlace { get; set; }
  public string Country { get; set; }
  public string Nation { get; set; }
  public string IDNumber { get; set; }
  public string SecurityNo { get; set; }
  public string Workunits { get; set; }
  public string Address { get; set; }
  public string ZIPCode { get; set; }
  public string Phone { get; set; }
  public string ContactPerson { get; set; }
  public string ContactShip { get; set; }
  public string ContactPersonAdd { get; set; }
  public string ContactPersonPhone { get; set; }
  public string OperationCode { get; set; }
  public string OperationName { get; set; }
  public string OperationTime { get; set; }
  public string CardNo { get; set; }
  public string ChangeType { get; set; }

 }
Salin selepas log masuk

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


Request patientIn = new Request();
   patientIn.System = "HIS";
   patientIn.SecurityCode = "HIS5";

   PatientBasicInfo basicInfo = new PatientBasicInfo();
   basicInfo.PatientNo = "1234";
   basicInfo.PatientName = "测试";
   basicInfo.Phoneticize = "";
   basicInfo.Sex = "1";
   basicInfo.Birth = "";
   basicInfo.BirthPlace = "";
   basicInfo.Country = "";
   basicInfo.Nation = "";
   basicInfo.IDNumber = "";
   basicInfo.SecurityNo = "";
   basicInfo.Workunits = "";
   basicInfo.Address = "";
   basicInfo.ZIPCode = "";
   basicInfo.Phone = "";
   basicInfo.ContactShip = "";
   basicInfo.ContactPersonPhone = "";
   basicInfo.ContactPersonAdd = "";
   basicInfo.ContactPerson = "";
   basicInfo.ChangeType = "";
   basicInfo.CardNo = "";
   basicInfo.OperationCode = "";
   basicInfo.OperationName = "";
   basicInfo.OperationTime = "";

   patientIn.PatientInfo = basicInfo;

   //序列化
   string strxml = XmlSerializeHelper.XmlSerialize<Request>(patientIn);
Salin selepas log masuk

3、生成的XML实例


<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <System>HIS</System>
 <SecurityCode>HIS5</SecurityCode>
 <PatientInfo>
 <PatientNo>1234</PatientNo>
 <PatientName>测试</PatientName>
 <Phoneticize />
 <Sex>1</Sex>
 <Birth />
 <BirthPlace />
 <Country />
 <Nation />
 <IDNumber />
 <SecurityNo />
 <Workunits />
 <Address />
 <ZIPCode />
 <Phone />
 <ContactPerson />
 <ContactShip />
 <ContactPersonAdd />
 <ContactPersonPhone />
 <OperationCode />
 <OperationName />
 <OperationTime />
 <CardNo />
 <ChangeType />
 </PatientInfo>
</Request>
Salin selepas log masuk

二、将XML转换成实体类

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


public static T DESerializer<T>(string strXML) where T:class
{
  try
 {
   using (StringReader sr = new StringReader(strXML))
   {
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    return serializer.Deserialize(sr) as T;
   }
  }
  catch (Exception ex)
  {
   return null;
  }
}
Salin selepas log masuk

示例:

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


//反序列化
Request r = XmlSerializeHelper.DESerializer<Request>(strxml);
Salin selepas log masuk

Atas ialah kandungan terperinci C#实现实体类和XML相互转换的示例代码详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan