C realizes the conversion between entity class and XML

  • 2021-12-11 08:36:50
  • OfStack

1. Entity classes are converted to XML

Converting an entity class to an XML requires serialization of the entity class using the Serialize method of the XmlSerializer class


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();
  }
}

Example:

1. Define entity classes


[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; }

 }

2. Assign a value to the entity class and convert the entity class to a string in XML format by serialization


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

   PatientBasicInfo basicInfo = new PatientBasicInfo();
   basicInfo.PatientNo = "1234";
   basicInfo.PatientName = " Test ";
   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;

   // Serialization 
   string strxml = XmlSerializeHelper.XmlSerialize<Request>(patientIn);

3. Generated XML instance


<?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> Test </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>

2. Convert XML to Entity Class

To convert XML into corresponding entity class, we need to use Deserialize method of XmlSerializer class to 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;
  }
}

Example:

Deserialize the serialized XML in the above example into an entity class


// Deserialization 
Request r = XmlSerializeHelper.DESerializer<Request>(strxml);

Related articles: