Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion between entity Class and XML in C #

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "C# how to achieve the conversion between entity classes and XML", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve the conversion between entity classes and XML" this article.

1. Convert entity classes to XML

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

Public static string XmlSerialize (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 the entity class [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 }} / / [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 } 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 the string Request patientIn = new Request (); patientIn.System = "HIS"; patientIn.SecurityCode = "HIS5"; PatientBasicInfo basicInfo = new PatientBasicInfo (); basicInfo.PatientNo = "1234"; basicInfo.PatientName = "Test"; basicInfo.Phoneticize = "" by serialization 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 (patientIn); 3, generated XML instance HIS HIS5 1234 test 1 2, convert XML to entity class

To convert the XML to the corresponding entity class, you need to deserialize the XML using the Deserialize method of the XmlSerializer class.

Public static T DESerializer (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

/ / deserialize Request r = XmlSerializeHelper.DESerializer (strxml); third, convert DataTable to XML// and convert DataTable to XMLDataTable dt = new DataTable ("MyTable"); / / add columns dt.Columns.Add ("Id", typeof (int)); dt.Columns.Add ("Name", typeof (string)); dt.Columns.Add ("Sex", typeof (char)); / / add rows dt.Rows.Add (1, "Xiaoming",'1') Dt.Rows.Add (2, "Xiao Hong",'2'); dt.Rows.Add (3, "Xiao Wang",'2'); dt.Rows.Add (4, "test",'2'); / / serialization, converting DataTable to XML format string string strXML = XmlSerializeHelper.XmlSerialize (dt); fourth, converting XML to DataTable// deserialization, converting XML to string DataTable dtNew= XmlSerializeHelper.DESerializer (strXML) Convert the List collection to XML/// test class / / public class Student {public int Id {get; set;} public string Name {get; set;} public char Sex {get; set;} public int Age {get; set }} / / Test set List list = new List () {Id=1,Name= "Xiao Hong", Sex='2',Age=20}, new Student () {Id=2,Name= "Xiao Ming", Sex='1',Age=22}, new Student () {Id=3,Name= "Xiao Wang", Sex='1',Age=19}, new Student () {Id=4,Name= "Test", Sex='2',Age=23}} / / serialize string strXML = XmlSerializeHelper.XmlSerialize (list); convert XML to a collection

Deserialize using the XML converted from the collection in the above example.

/ / deserialize List listStu = XmlSerializeHelper.DESerializer (strXML); this is all the content of the article "how to convert entity classes to XML in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report