C # example code for implementing entity classes and XML conversions to each other

Source: Internet
Author: User
This article mainly for you to introduce the C # Implementation of entity classes and XML conversion of data, with a certain reference value, interested in small partners can refer to

First, the entity class is converted into XML

Converting an entity class to XML requires using the Serialize method of the XmlSerializer class to serialize the entity 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. Defining entity Classes


[System.Xml.Serialization.XmlTypeAttribute (Anonymoustype = True)] [System.Xml.Serialization.XmlRootAttribute (Namespace = "", IsNullable = False)] public class Request {public string Syst  em {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 an XML-formatted string 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; Serialize 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/> & Lt Nation/> <idnumber/> <securityno/> <workunits/> <address/> <ZIPCode/> <Phone/&G T <contactperson/> <contactship/> <contactpersonadd/> <contactpersonphone/> < Operationcode/> <operationname/> <operationtime/> <cardno/> <ChangeType/> </ Patientinfo></request>

Ii. converting XML to entity classes

converting XML to the corresponding entity class requires using 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 Stringreade R (strxml))   {    XmlSerializer serializer = new XmlSerializer (typeof (T));    Return serializer. Deserialize (SR) as T;   }  }  catch (Exception ex)  {   return null;  }}

Example:

Deserializes the serialized XML from the previous example into an entity class


Deserialization Request r = xmlserializehelper.deserializer<request> (strxml);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.