zoukankan      html  css  js  c++  java
  • XmlElement可以避免由XmlSerializer多余生成的代码

    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>
  • 相关阅读:
    动态规划(DP),模拟
    递推,动态规划(DP),字符串处理,最佳加法表达式
    递归,动态规划,找最短路径,Help Jimmy
    动规,模拟,递推,最长公共子序列
    递归,递推,记忆化搜索,空间优化(数字三角形)
    递归,记忆化搜索,(棋盘分割)
    递归,搜索,回溯,最优路径(线段)
    web前端开发初学者必看的学习路线(附思维导图)
    一位资深程序员大牛给予Java初学者的学习路线建议
    假如时光倒流,我会这么学习Java
  • 原文地址:https://www.cnblogs.com/sskset/p/3300650.html
Copyright © 2011-2022 走看看