• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

XML之序列化C#实体类,DataTable,List

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.

static void Main(string[] args)
        {
            #region 实体类
            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;

            //序列化为xml
            string strxml = Class_to_xml.XmlSerialize<Request>(patientIn);
            //Console.Write(strxml);

            //反序列化为实体类
            Request r = Class_to_xml.DESerializer<Request>(strxml);
            //Console.Write(r);
             #endregion

             #region DataTable
             //将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');
             //序列化为xml,将DataTable转换成XML格式的字符串
             string strXML = Class_to_xml.XmlSerialize<DataTable>(dt);
             //Console.Write(strXML);

             //反序列化为DataTable
             DataTable dtNew = Class_to_xml.DESerializer<DataTable>(strXML);
             Console.Write(strXML);
             #endregion

             #region 列表集合
             //测试集合
             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}
            };
             //序列化为xml
             string strXML = Class_to_xml.XmlSerialize<List<Student>>(list);
             Console.Write(strxml);

             //反序列化为list
             List<Student> listStu = Class_to_xml.DESerializer<List<Student>>(strXML);
             foreach (var item in listStu)
             {
                 Console.WriteLine(item.Age);
             }
             #endregion
            
             Console.ReadKey();
        }

2.

public class Class_to_xml
    {
        //实体类转换XML,xml序列化
        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();
            }
        }
        //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;
            }
        }


    }
Class_to_xml

3.

 [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; }

    }
    /// <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; }
    }
实体类

 4.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 效果

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
接口C#请求数据模拟POST发布时间:2022-07-10
下一篇:
C#下开发手机使用的jar发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap