国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現(xiàn)實體類和XML相互轉(zhuǎn)換

2019-10-29 21:12:38
字體:
供稿:網(wǎng)友

一、實體類轉(zhuǎn)換成XML

將實體類轉(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();  }}

示例:

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; }   } /// <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、給實體類賦值,并通過序列化將實體類轉(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實例

<?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)換成實體類

把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);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 祁阳县| 彭山县| 新昌县| 嘉祥县| 城口县| 新津县| 开鲁县| 吉首市| 竹北市| 方正县| 安泽县| 长宁区| 老河口市| 汾西县| 志丹县| 雷波县| 新邵县| 开平市| 门头沟区| 沂南县| 高阳县| 黔东| 浠水县| 浮山县| 通化县| 湘阴县| 洛隆县| 甘孜| 赤水市| 宜昌市| 谷城县| 溧阳市| 青冈县| 闻喜县| 关岭| 临安市| 德保县| 峨边| 呼图壁县| 炎陵县| 怀集县|