C#實現實體類和XML相互轉換的範例程式碼詳解

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了C#實現實體類和XML相互轉換的資料,具有一定的參考價值,感興趣的小夥伴們可以參考一下

一、實體類轉換成XML

將實體類轉換成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、給實體類賦值,並通過序列化將實體類轉換成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轉換成實體類

把XML轉換成相應的實體類,需要使用到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);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.