K3

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public class Program
    {
        static void Main(string[] args)
        {
            var alarm = new Alarm()
            {
                Code = "1588-OPER-2",
                Grade = "1",
                Source = "55A3E6C7E2A0560A956D3933F42E5AA2",
                Time = DateTime.Now,
                Text = "Operationnal state is offline."
            };

            var alarm1 = new Alarm()
            {
                Code = "1588-OPER-2",
                Grade = "2",
                Source = "55A3E6C7E2A0560A956D3933F42E5AA2",
                Time = DateTime.Now,
                Text = "Operationnal state is offline."
            };

            var alarms = new AlarmSet("wxboss.srm.alarm", new List<Alarm> { alarm, alarm1 });


            var alarmFile = new AlarmCatalog();
            alarmFile.Alarms = new List<AlarmSet> { alarms };

            Generate(alarmFile);

        }

        public static void Generate(AlarmCatalog file)
        {
            string path = "C:\\test.xml";

            Stream stream = new FileStream(path, FileMode.Create);

            var serializer = new XmlSerializer(typeof(AlarmCatalog));
            serializer.Serialize(stream, file);

            stream.Close();
        }

        [XmlRoot("dc")]
        public class AlarmCatalog
        {
            [XmlElement("mo")]
            public List<AlarmSet> Alarms { get; set; }
        }

        public class AlarmSet
        {
            #region Initialization
            public AlarmSet()
            {

            }

            public AlarmSet(string domain)
            {
                this.Domain = domain;
            }

            public AlarmSet(string domain, List<Alarm> items)
            {
                this.Domain = domain;
                this.Items = items;
            }
            #endregion

            [XmlAttribute("dn")]
            public string Domain { get; set; }

            [XmlElement("alarm")]
            public List<Alarm> Items { get; set; }
        }

        public class Alarm
        {
            public Alarm() { }

            public Alarm(string text)
            {
                this.Text = text;
            }

            [XmlAttribute("source")]
            public string Source { get; set; }

            [XmlAttribute("grade")]
            public string Grade { get; set; }

            [XmlAttribute("code")]
            public string Code { get; set; }

            [XmlIgnore]
            public DateTime Time { get; set; }

            [XmlAttribute("time")]
            public string TimeString
            {
                get { return this.Time.ToString("yyyy-MM-dd HH:mm:ss"); }
                set { this.Time = DateTime.Parse(value); }
            }

            [XmlText]
            public string Text { get; set; }
        }
    }
<?xml version="1.0"?><dc xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<mo dn="wxboss.srm.alarm">
  <
alarm time="2013-09-04 11:33:01" code="1588-OPER-2" grade="1" source="55A3E6C7E2A0560A956D3933F42E5AA2">Operationnal state is offline.</alarm>
  <
alarm time="2013-09-04 11:33:01" code="1588-OPER-2" grade="2" source="55A3E6C7E2A0560A956D3933F42E5AA2">Operationnal state is offline.</alarm>
</
mo>

</
dc>

 

posted on 2013-09-04 11:37  K3  阅读(564)  评论(0编辑  收藏  举报