網站首頁 編程語言 正文
一、實體類轉換成XML
將實體類轉換成XML需要使用XmlSerializer類的Serialize方法,將實體類序列化
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(); } }
示例:
1、定義實體類
[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 { 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、給實體類賦值,并通過序列化將實體類轉換成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(patientIn);
3、生成的XML實例
HIS HIS5 1234 測試 1
二、將XML轉換成實體類
把XML轉換成相應的實體類,需要使用到XmlSerializer類的Deserialize方法,將XML進行反序列化。
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; } }
示例:
將上例中序列化后的XML反序列化成實體類
//反序列化 Request r = XmlSerializeHelper.DESerializer(strxml);
?三、將DataTable轉換成XML
//將DataTable轉換成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轉換成XML格式的字符串 string strXML = XmlSerializeHelper.XmlSerialize(dt);
四、將XML轉換成DataTable
//反序列化,將XML轉換成字符串 DataTable dtNew= XmlSerializeHelper.DESerializer(strXML);
五、將List集合轉換成XML
////// 測試類 /// public class Student { public int Id { get; set; } public string Name { get; set; } public char Sex { get; set; } public int Age { get; set; } } //測試集合 Listlist = new List () { 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);
六、將XML轉換成集合
使用上面例子中集合轉換成的XML進行反序列化。
//反序列化 ListlistStu = XmlSerializeHelper.DESerializer >(strXML);
原文鏈接:https://www.cnblogs.com/dotnet261010/p/6513618.html
相關推薦
- 2022-09-05 UserWarning: indexing with dtype torch.uint8 is no
- 2023-01-19 C++深度探索虛函數指針示例_C 語言
- 2022-10-23 python操作SqlServer獲取特定表的所有列名(推薦)_python
- 2023-01-26 Kotlin協程Channel源碼示例淺析_Android
- 2022-06-17 基于pgrouting的路徑規劃處理方法_PostgreSQL
- 2022-12-09 C++中利用cout和fstream采用非科學計數法輸出_C 語言
- 2022-03-22 C++using聲明和using編譯指令_C 語言
- 2022-10-29 【npm 報錯 gyp info it worked if it ends with ok 大概率是
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支