您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C#怎么實現(xiàn)實體類和XML的相互轉(zhuǎn)換”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C#怎么實現(xiàn)實體類和XML的相互轉(zhuǎn)換”這篇文章吧。
將實體類轉(zhuǎn)換成XML需要使用XmlSerializer類的Serialize方法,將實體類序列化
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(); } }
示例:
[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; } }
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);
<?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)換成相應(yīng)的實體類,需要使用到XmlSerializer類的Deserialize方法,將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 serializer.Deserialize(sr) as T; } } catch (Exception ex) { return null; } }
示例:
將上例中序列化后的XML反序列化成實體類
//反序列化 Request r = XmlSerializeHelper.DESerializer<Request>(strxml);
//將DataTable轉(zhuǎn)換成XML DataTable dt = new DataTable("MyTable"); //添加列 dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Sex", typeof(char)); //添加行 dt.Rows.Add(1, "小明", '1'); dt.Rows.Add(2, "小紅", '2'); dt.Rows.Add(3, "小王", '2'); dt.Rows.Add(4, "測試", '2'); //序列化,將DataTable轉(zhuǎn)換成XML格式的字符串 string strXML = XmlSerializeHelper.XmlSerialize <DataTable> (dt);
//反序列化,將XML轉(zhuǎn)換成字符串 DataTable dtNew= XmlSerializeHelper.DESerializer<DataTable>(strXML);
/// <summary> /// 測試類 /// </summary> public class Student { public int Id { get; set; } public string Name { get; set; } public char Sex { get; set; } public int Age { get; set; } } //測試集合 List<Student> list = new List<Student>() { new Student(){Id=1,Name="小紅",Sex='2',Age=20}, new Student(){Id=2,Name="小明",Sex='1',Age=22}, new Student(){Id=3,Name="小王",Sex='1',Age=19}, new Student(){Id=4,Name="測試",Sex='2',Age=23} }; //序列化 string strXML = XmlSerializeHelper.XmlSerialize<List<Student>>(list);
使用上面例子中集合轉(zhuǎn)換成的XML進行反序列化。
//反序列化 List<Student> listStu = XmlSerializeHelper.DESerializer<List<Student>>(strXML);
以上是“C#怎么實現(xiàn)實體類和XML的相互轉(zhuǎn)換”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。