SoFunction
Updated on 2025-03-07

C# implements the mutual conversion of entity classes and XML

1. Convert entity classes to XML

Converting entity classes to XML requires the use of the Serialize method of the XmlSerializer class to serialize entity classes.

public static string XmlSerialize<T>(T obj)
{
  using (StringWriter sw = new StringWriter())
  {
    Type t= ();    
    XmlSerializer serializer = new XmlSerializer(());
    (sw, obj);
    ();
    return ();
  }
}

Example:

1. Define entity class

[(AnonymousType = true)]
 [(Namespace = "", IsNullable = false)]
 public class Request
 {

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

 /// <remarks/>
 [(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 values ​​to the entity class and convert the entity class into XML format strings through serialization

Request patientIn = new Request();
    = "HIS";
    = "HIS5";

   PatientBasicInfo basicInfo = new PatientBasicInfo();
    = "1234";
    = "test";
    = "";
    = "1";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";
    = "";

    = basicInfo;

   //Serialization   string strxml = &lt;Request&gt;(patientIn);

3. Generated XML instance

&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Request xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:xsd="http:///2001/XMLSchema"&gt;
 &lt;System&gt;HIS&lt;/System&gt;
 &lt;SecurityCode&gt;HIS5&lt;/SecurityCode&gt;
 &lt;PatientInfo&gt;
 &lt;PatientNo&gt;1234&lt;/PatientNo&gt;
 &lt;PatientName&gt;test&lt;/PatientName&gt;
 &lt;Phoneticize /&gt;
 &lt;Sex&gt;1&lt;/Sex&gt;
 &lt;Birth /&gt;
 &lt;BirthPlace /&gt;
 &lt;Country /&gt;
 &lt;Nation /&gt;
 &lt;IDNumber /&gt;
 &lt;SecurityNo /&gt;
 &lt;Workunits /&gt;
 &lt;Address /&gt;
 &lt;ZIPCode /&gt;
 &lt;Phone /&gt;
 &lt;ContactPerson /&gt;
 &lt;ContactShip /&gt;
 &lt;ContactPersonAdd /&gt;
 &lt;ContactPersonPhone /&gt;
 &lt;OperationCode /&gt;
 &lt;OperationName /&gt;
 &lt;OperationTime /&gt;
 &lt;CardNo /&gt;
 &lt;ChangeType /&gt;
 &lt;/PatientInfo&gt;
&lt;/Request&gt;

2. Convert XML to entity class

To convert XML into the corresponding entity class, you need to use the Deserialize method of the XmlSerializer class to deserialize the 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 (sr) as T;
   }
  }
  catch (Exception ex)
  {
   return null;
  }
}

Example:

Deserialize the serialized XML in the above example into entity class

//DeserializationRequest r = &lt;Request&gt;(strxml);

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.