zoukankan      html  css  js  c++  java
  • c#XML转换成实体

    首选:我的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; }    
  • 相关阅读:
    spring-boot 参考链接
    Java中所涉及到的设计模式小记
    C# Xamarin开发 GenyMotion adb List of devices attached
    Java中的instanceof关键字
    Struts2文件下载
    Spring3基础原理解析
    JSP和Struts2、Hibernate、Spring3基础内容和原理
    Struts2多文件上传原理和示例
    Struts2单文件上传原理及示例
    Struts2国际化学习笔记
  • 原文地址:https://www.cnblogs.com/youngharvard/p/8657484.html
Copyright © 2011-2022 走看看