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

c#XML转换成实体

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

首选:我的XML文件如下(mysql表里导出的):几千条,就选2条做例子。

<?xml version="1.0" encoding="utf-8" ?>
<RECORDS>
<RECORD>
<CardId>13</CardId>
<CardNo>DSL0000011</CardNo>
<IdNo>321283198410201416</IdNo>
<Name>林黛玉</Name>
<BrithDay>1955/7/13</BrithDay>
<Sex>M</Sex>
<Phone></Phone>
<Balance>3065.84</Balance>
<Point>600</Point>
<Level>普通卡</Level>
<ActiveDeviceSN>98261720412341</ActiveDeviceSN>
<ActiveStaffName>贾母</ActiveStaffName>
<ActiveTime>2017/11/3 11:52:00</ActiveTime>
<LatestUseTime>2017/12/19 10:37:14</LatestUseTime>
<ExpireTime>2027/11/3 11:52:00</ExpireTime>
<Status>0</Status>
<CreateBy></CreateBy>
<CreateTime>2017/11/3 11:52:00</CreateTime>
<UpdateBy>1</UpdateBy>
<UpdateTime>2017/11/3 14:15:04</UpdateTime>
<IsDelete>0</IsDelete>
</RECORD>
<RECORD>
<CardId>14</CardId>
<CardNo>DSL0000011</CardNo>
<IdNo>321283198410201416</IdNo>
<Name>贾宝玉</Name>
<BrithDay>1992/7/13</BrithDay>
<Sex>M</Sex>
<Phone></Phone>
<Balance>36.15</Balance>
<Point>200</Point>
<Level>银卡</Level>
<ActiveDeviceSN>98261720412341</ActiveDeviceSN>
<ActiveStaffName>贾雨村</ActiveStaffName>
<ActiveTime>2017/11/3 11:52:00</ActiveTime>
<LatestUseTime>0001/1/1 00:00:00</LatestUseTime>
<ExpireTime>2027/11/3 11:52:00</ExpireTime>
<Status>0</Status>
<CreateBy></CreateBy>
<CreateTime>2017/11/3 11:52:00</CreateTime>
<UpdateBy>1</UpdateBy>
<UpdateTime>2017/11/3 14:15:04</UpdateTime>
<IsDelete>0</IsDelete>
</RECORD>
</RECORDS>

建的实体类如下:

    [XmlRoot("RECORDS")]
    public class SetDataModel
    {
        [XmlElement("RECORD")]

        public List<OldCard> Cards { get; set; }
    }

    [XmlType("RECORD")]
    public class OldCard
    {
        public int CardId { get; set; }
        public string CardNo { get; set; }
        public string IdNo { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public decimal? Balance { get; set; }
        public int? Point { get; set; }
        public string Level { get; set; }
        public string ActiveDeviceSN { get; set; }
        public string ActiveStaffName { get; set; }
        [XmlElement("ActiveTime")]
        public string ActiveTime2
        {
            get { return ActiveTime.Value.ToString("yyyy-MM-dd HH:mm:ss"); }
            set { ActiveTime = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public Nullable<System.DateTime> ActiveTime { get; set; }
        [XmlElement("LatestUseTime")]
        public string LatestUseTime2
        {
            get { return LatestUseTime.Value.ToString("yyyy-MM-dd HH:mm:ss"); }
            set { LatestUseTime = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public Nullable<System.DateTime> LatestUseTime { get; set; }
        [XmlElement("ExpireTime")]
        public string ExpireTime2
        {
            get { return ExpireTime.Value.ToString("yyyy-MM-dd HH:mm:ss"); }
            set { ExpireTime = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public Nullable<System.DateTime> ExpireTime { get; set; }
        public int? Status { get; set; }
        [XmlElement("BrithDay")]
        public string BrithDay2
        {
            get { return BrithDay.Value.ToString("yyyy-MM-dd HH:mm:ss"); }
            set { BrithDay = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public DateTime? BrithDay { get; set; }
        public string StatusName { get; set; }


      
        [XmlElement("CreateBy")]
        public string CreateByStr
        {
            get { return CreateBy.ToString(); }
            set { if (value == "") CreateBy = 0; else CreateBy = Convert.ToInt32(value); }
        }
        [XmlIgnore]
        public int CreateBy { get; set; }
        [XmlElement("CreateTime")]
        public string CreateTime2
        {
            get {
               
                    return CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
               
            }
            set { CreateTime = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public Nullable<System.DateTime> CreateTime { get; set; }
        [XmlElement("UpdateBy")]
        public string UpdateByStr
        {
            get { return UpdateBy.ToString(); }
            set { if (value == "") UpdateBy = 0; else UpdateBy = Convert.ToInt32(value); }
        }
        [XmlIgnore]
        public Nullable<int> UpdateBy { get; set; }
        [XmlElement("UpdateTime")]
        public string UpdateTime2
        {
            get
            {   
                 return UpdateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            set { UpdateTime = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public Nullable<System.DateTime> UpdateTime { get; set; }
        public bool IsDelete { get; set; }
        

    }
View Code

 

实体赋值的方法如下:

      public void SetCardData()
        {

            //StreamReader sr = new StreamReader(@"d:\Card.json");
            //string json = sr.ReadToEnd();
//var data = Newtonsoft.Json.JsonConvert.DeserializeObject<SetDataModel>(json); StreamReader sr = new StreamReader(@"d:\Card.xml"); string xml = sr.ReadToEnd(); sr.Close(); var data = DESerializer<SetDataModel>(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; } }

 困扰1:时间类型,转换直接报错

字符串“2017/11/3 11:52:00”不是有效的 AllXsd 值。

所以才要把

public Nullable<System.DateTime> CreateTime { get; set; }

写成:

        [XmlElement("CreateTime")]
        public string CreateTime2
        {
            get {
               
                    return CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
               
            }
            set { CreateTime = DateTime.Parse(value); }
        }
        [XmlIgnore]
        public Nullable<System.DateTime> CreateTime { get; set; }    

 

困扰2:int类型xml不能为空  比如 

<CreateBy></CreateBy>

为空 直接报错:输入字符串的格式不正确。

所以要把

public int CreateBy { get; set; }

写成:

        [XmlElement("CreateBy")]
        public string CreateByStr
        {
            get { return CreateBy.ToString(); }
            set { if (value == "") CreateBy = 0; else CreateBy = Convert.ToInt32(value); }
        }
        [XmlIgnore]
        public int CreateBy { get; set; }    

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件发布时间:2022-07-13
下一篇:
C#中对于接口的实现方式发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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