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 = <Request>(patientIn);
3. Generated XML instance
<?xml version="1.0" encoding="utf-16"?> <Request xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:xsd="http:///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 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 = <Request>(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.