Xml序列化

Xml格式:

<?xml version="1.0" encoding="utf-8" ?>
<Monitors>
  <Monitor>
    <ID>1</ID>
    <Name>新期监控</Name>
    <!--是否监控-->
    <IsEnable>1</IsEnable>
    <!--监控时间间隔,单位min-->
    <MonitorInterval>10</MonitorInterval>
    <!--警报时间界限,单位min-->
    <AlarmTime>-100</AlarmTime>
    <!--推送记录缓存时间,单位小时-->
    <CacheExpireTime>24</CacheExpireTime>
    <!--通知接收人-->
    <NoticeRecipient>
      <Phone>13613048827</Phone>
    </NoticeRecipient>
    <!--玩法-->
    <Games>
      <Game>K3</Game>
    </Games>
  </Monitor>
  <Monitor>
    <ID>2</ID>
    <Name>开奖公告监控</Name>
    <!--是否监控-->
    <IsEnable>1</IsEnable>
    <!--监控时间间隔,单位min-->
    <MonitorInterval>15</MonitorInterval>
    <!--警报时间界限,单位min-->
    <AlarmTime>10</AlarmTime>
    <!--推送记录缓存时间,单位小时-->
    <CacheExpireTime>24</CacheExpireTime>
    <!--通知接收人-->
    <NoticeRecipient>
      <Phone>13613048827</Phone>
    </NoticeRecipient>
    <!--玩法-->
    <Games>
      <Game>K3</Game>
    </Games>
  </Monitor>
  <Monitor>
    <ID>3</ID>
    <Name>余额监控</Name>
    <!--是否监控-->
    <IsEnable>1</IsEnable>
    <!--监控时间间隔,单位min-->
    <MonitorInterval>20</MonitorInterval>
    <!--推送记录缓存时间,单位小时-->
    <CacheExpireTime>2</CacheExpireTime>
    <!--通知接收人-->
    <NoticeRecipient>
      <Phone>13613048827</Phone>
    </NoticeRecipient>
    <!--逻辑号-->
    <LogicCodes>
      <LogicCode>
        <Code>36010017</Code>
        <!--小于报警-->
        <BalanceLimit>100000</BalanceLimit>
      </LogicCode>
    </LogicCodes>
  </Monitor>
</Monitors>

 

实体类:类都用[XmlType("Monitors")],初始化elementName不能省略!属性如果是单个用[XmlElement("Monitor")],若果是集合用[XmlArray("LogicCodes", IsNullable = false)],如果定义的属性名和Xml节点名一样可以省略初始化elementName!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsigneeMonitorService.Model
{
    [XmlType("Monitors")]
    public class Monitors
    {
        [XmlElement("Monitor")]
        public List<Monitor> MonitorList { get; set; }
    }

    [XmlType("Monitor")]
    public class Monitor
    {
        [XmlElement("ID")]
        public int Id { get; set; }
        [XmlElement]
        public string Name { get; set; }
        [XmlElement]
        public int IsEnable { get; set; }
        [XmlElement]
        public double MonitorInterval { get; set; }
        [XmlElement]
        public double AlarmTime { get; set; }
        [XmlElement]
        public double CacheExpireTime { get; set; }
        [XmlElement]
        public NoticeRecipient NoticeRecipient { get; set; }
        [XmlElement(IsNullable = false)]
        public Games Games { get; set; }
        [XmlArray("LogicCodes", IsNullable = false)]
        public List<LogicCode> LogicCodes { get; set; }
    }

    [XmlType("NoticeRecipient")]
    public class NoticeRecipient
    {
        [XmlElement]
        public List<string> Phone { get; set; }
    }

    [XmlType("Games")]
    public class Games
    {
        [XmlElement]
        public List<string> Game { get; set; }
    }

    [XmlType("LogicCode")]
    public class LogicCode
    {
        [XmlElement]
        public string Code { get; set; }
        [XmlElement]
        public decimal BalanceLimit { get; set; }
    }
}

 

反序列化方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;

namespace ConsigneeMonitorService.Common
{
    public class XmlHelper
    {
        public static T LoadFromXml<T>(string path)
        {
            if (path == null) throw new Exception("XML文件反序列化,路径为空!");
            if (!File.Exists(path)) throw new Exception("XML文件反序列化,文件不存在!");
            T t = default(T);
            using (StreamReader reader=new StreamReader(path))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                t = (T)serializer.Deserialize(reader);
            }
            return t;
        }
    }
}

 

posted @ 2016-11-15 20:12  花生打代码会头痛  阅读(123)  评论(0)    收藏  举报