最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

將實(shí)體類轉(zhuǎn)換成XML需要使用XmlSerializer類的Serialize方法,將實(shí)體類序列化

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、定義實(shí)體類

[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、給實(shí)體類賦值,并通過(guò)序列化將實(shí)體類轉(zhuǎn)換成XML格式的字符串

Request patientIn = new Request();
            patientIn.System = "HIS";
            patientIn.SecurityCode = "HIS5";

            PatientBasicInfo basicInfo = new PatientBasicInfo();
            basicInfo.PatientNo = "1234";
            basicInfo.PatientName = "測(cè)試";
            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實(shí)例

<?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>測(cè)試</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)換成實(shí)體類

把XML轉(zhuǎn)換成相應(yīng)的實(shí)體類,需要使用到XmlSerializer類的Deserialize方法,將XML進(jìn)行反序列化。

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反序列化成實(shí)體類

//反序列化
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, "測(cè)試", '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>
/// 測(cè)試類
/// </summary>
public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public char Sex { get; set; }
    public int Age { get; set; }
}

//測(cè)試集合
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="測(cè)試",Sex='2',Age=23}
};
//序列化
string strXML = XmlSerializeHelper.XmlSerialize<List<Student>>(list);

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

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

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

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

相關(guān)文章

  • C#集合本質(zhì)之堆棧的用法詳解

    C#集合本質(zhì)之堆棧的用法詳解

    本文詳細(xì)講解了C#集合本質(zhì)之堆棧的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹(shù)的方法

    C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹(shù)的方法

    這篇文章主要介紹了C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹(shù)的方法,涉及C#遍歷二叉樹(shù)的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • 基于WPF實(shí)現(xiàn)帶明細(xì)的環(huán)形圖表

    基于WPF實(shí)現(xiàn)帶明細(xì)的環(huán)形圖表

    這篇文章主要介紹了如何利用WPF繪制帶明細(xì)的環(huán)形圖表?,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-08-08
  • C#程序調(diào)用cmd.exe執(zhí)行命令

    C#程序調(diào)用cmd.exe執(zhí)行命令

    這篇文章介紹了C#程序調(diào)用cmd.exe執(zhí)行命令的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • c# 如何實(shí)現(xiàn)代碼生成器

    c# 如何實(shí)現(xiàn)代碼生成器

    這篇文章主要介紹了c# 如何實(shí)現(xiàn)代碼生成器,幫助大家更好的理解和使用c# 編程語(yǔ)言,感興趣的朋友可以了解下
    2020-12-12
  • C#判斷多個(gè)文本框是否為空的方法

    C#判斷多個(gè)文本框是否為空的方法

    這篇文章主要介紹了C#判斷多個(gè)文本框是否為空的方法,可實(shí)現(xiàn)對(duì)多個(gè)文本框的遍歷、判斷及提示等功能,需要的朋友可以參考下
    2015-06-06
  • C# GetField方法的應(yīng)用實(shí)例講解

    C# GetField方法的應(yīng)用實(shí)例講解

    C#中的GetField是一個(gè)反射方法,用于獲取指定類型的字段信息,它可以通過(guò)字段名稱來(lái)獲取字段對(duì)象,并且可以在運(yùn)行時(shí)動(dòng)態(tài)地訪問(wèn)和操作這些字段,本文給大家介紹了C# GetField方法的應(yīng)用,需要的朋友可以參考下
    2024-04-04
  • C#獲取web.config配置文件內(nèi)容的方法

    C#獲取web.config配置文件內(nèi)容的方法

    這篇文章主要介紹了C#獲取web.config配置文件內(nèi)容的方法,涉及C#配置文件屬性獲取的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法

    C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法

    下面小編就為大家?guī)?lái)一篇C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • C# TreeView控件使用代碼

    C# TreeView控件使用代碼

    TreeView控件的實(shí)例代碼,需要的朋友可以參考下。
    2009-09-09

最新評(píng)論

广汉市| 光山县| 湘乡市| 衡南县| 泌阳县| 来宾市| 吉木乃县| 迭部县| 蒙阴县| 丰镇市| 滨州市| 晋城| 股票| 讷河市| 吴旗县| 青铜峡市| 县级市| 大洼县| 兰考县| 纳雍县| 济源市| 依安县| 尤溪县| 盐山县| 大新县| 珲春市| 荣成市| 旌德县| 新巴尔虎右旗| 赫章县| 玛曲县| 灌南县| 开鲁县| 当雄县| 阳江市| 无为县| 怀宁县| 南投县| 北海市| 湘阴县| 江永县|