C#實(shí)現(xiàn)實(shí)體類和XML相互轉(zhuǎn)換
一、實(shí)體類轉(zhuǎn)換成XML
將實(shí)體類轉(zhuǎn)換成XML需要使用XmlSerializer類的Serialize方法,將實(shí)體類序列化
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(); } }
示例:
1、定義實(shí)體類
[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、給實(shí)體類賦值,并通過序列化將實(shí)體類轉(zhuǎn)換成XML格式的字符串
Request patientIn = new Request(); patientIn.System = "HIS"; patientIn.SecurityCode = "HIS5"; PatientBasicInfo basicInfo = new PatientBasicInfo(); basicInfo.PatientNo = "1234"; basicInfo.PatientName = "測試"; 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; //序列化 string strxml = XmlSerializeHelper.XmlSerialize<Request>(patientIn);
3、生成的XML實(shí)例
<?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>測試</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>
二、將XML轉(zhuǎn)換成實(shí)體類
把XML轉(zhuǎn)換成相應(yīng)的實(shí)體類,需要使用到XmlSerializer類的Deserialize方法,將XML進(jìn)行反序列化。
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; } }
示例:
將上例中序列化后的XML反序列化成實(shí)體類
//反序列化 Request r = XmlSerializeHelper.DESerializer<Request>(strxml);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#后臺(tái)調(diào)用前臺(tái)JS函數(shù)方法
今天小編就為大家分享一篇關(guān)于C#后臺(tái)調(diào)用前臺(tái)JS函數(shù)方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01基于C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼
這篇文章主要介紹了C#實(shí)現(xiàn)的屏幕指定區(qū)域截屏代碼,有需要的朋友可以參考一下2014-01-01C#應(yīng)用XML作為數(shù)據(jù)庫的快速開發(fā)框架實(shí)現(xiàn)方法
這篇文章主要介紹了C#應(yīng)用XML作為數(shù)據(jù)庫的快速開發(fā)框架實(shí)現(xiàn)方法,詳細(xì)介紹了將XML作為數(shù)據(jù)庫的C#桌面應(yīng)用開發(fā)技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12淺析C#中數(shù)組,ArrayList與List對象的區(qū)別
在C#中,當(dāng)我們想要存儲(chǔ)一組對象的時(shí)候,就會(huì)想到用數(shù)組,ArrayList,List這三個(gè)對象了。那么這三者到底有什么樣的區(qū)別呢2013-07-07