欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年02月26日 09:35:22   作者:.NET開發(fā)菜鳥  
本文詳細講解了C#實現(xiàn)實體類和XML的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、實體類轉(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);

 三、將DataTable轉(zhuǎn)換成XML

//將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

//反序列化,將XML轉(zhuǎn)換成字符串
DataTable dtNew=  XmlSerializeHelper.DESerializer<DataTable>(strXML);

五、將List集合轉(zhuǎn)換成XML

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

六、將XML轉(zhuǎn)換成集合

使用上面例子中集合轉(zhuǎn)換成的XML進行反序列化。

//反序列化
List<Student> listStu = XmlSerializeHelper.DESerializer<List<Student>>(strXML);

到此這篇關(guān)于C#實現(xiàn)實體類和XML相互轉(zhuǎn)換的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#/VB.NET實現(xiàn)在Word文檔中添加頁眉和頁腳

    C#/VB.NET實現(xiàn)在Word文檔中添加頁眉和頁腳

    頁眉位于文檔中每個頁面的頂部區(qū)域,常用于顯示文檔的附加信息;頁腳位于文檔中每個頁面的底部的區(qū)域,常用于顯示文檔的附加信息。今天這篇文章就將為大家展示如何以編程的方式在在?Word?文檔中添加頁眉和頁腳
    2023-03-03
  • 詳解C#讀取Appconfig中自定義的節(jié)點

    詳解C#讀取Appconfig中自定義的節(jié)點

    我們往往需要在App.config中自定義一些節(jié)來滿足實際需要,而不依賴于App.config的appSettings,下面通過一個簡單的實例來說明自定義配置節(jié)點的設(shè)置與讀取
    2015-06-06
  • C#在Excel表格中插入、編輯和刪除批注

    C#在Excel表格中插入、編輯和刪除批注

    這篇文章主要為大家詳細介紹了C#如何在Excel表格中插入、編輯和刪除批注,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別

    C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別

    這篇文章主要給大家介紹了關(guān)于C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • unity實現(xiàn)場景切換進度條顯示

    unity實現(xiàn)場景切換進度條顯示

    這篇文章主要為大家詳細介紹了unity實現(xiàn)場景切換進度條顯示,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C# 8.0中的范圍類型(Range Type)示例詳解

    C# 8.0中的范圍類型(Range Type)示例詳解

    這篇文章主要給大家介紹了關(guān)于C# 8.0中范圍類型(Range Type)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • C# 線程相關(guān)知識總結(jié)

    C# 線程相關(guān)知識總結(jié)

    這篇文章主要介紹了C# 線程相關(guān)知識,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 在C#中如何使用正式表達式獲取匹配所需數(shù)據(jù)

    在C#中如何使用正式表達式獲取匹配所需數(shù)據(jù)

    本文給大家分享C#中如何使用正式表達式獲取匹配所需數(shù)據(jù) ,非常實用,對正則表達式獲取匹配相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • C#中WPF使用多線程調(diào)用窗體組件的方法

    C#中WPF使用多線程調(diào)用窗體組件的方法

    這篇文章主要介紹了C#中WPF使用多線程調(diào)用窗體組件的方法,涉及C#中多線程的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#如何獲取當(dāng)前路徑的父路徑

    C#如何獲取當(dāng)前路徑的父路徑

    這篇文章主要介紹了C#如何獲取當(dāng)前路徑的父路徑問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評論