Deserialize multiple XML elements with the same name through XmlSerializer class in C#

后端 未结 3 1186
萌比男神i
萌比男神i 2020-12-31 08:51

I have an XML in the form

 
    0  
    0  
           


        
相关标签:
3条回答
  • 2020-12-31 09:27

    You should just have to do the following for this to work:

    [XmlElement]
    public int[] ScheduledDay { get; set; }
    

    By adding this attribute, every time the ScheduledDay element is seen by the (de)serializer it will know to add it to this array.

    0 讨论(0)
  • 2020-12-31 09:28

    You don't want XmlArrayItem. You want the array of ints to be serialized without a parent element, which means you should decorate the array itself with XmlElement. Because you have a particular order, you will want to use the Order value on the XmlElement attribute. Here's the class, modified accordingly:

    public class BackupScheduleSettings
    {
        public BackupScheduleSettings()
        {
            ScheduledDay = new int[7];
        }
    
        [XmlElement(Order=1)]
        public int AggressiveMode;
        [XmlElement(Order=2)]
        public int ScheduleType;
        //[XmlArrayItem("ArrayWrapper")]
        [XmlElement(Order=3)]
        public int[] ScheduledDay { get; set; }
        [XmlElement(Order=4)]
        public int WindowStart;
        [XmlElement(Order=5)]
        public int WindowEnd;
        [XmlElement(Order=6)]
        public int ScheduleInterval;
    }
    

    Here's the generated xML:

    <BackupScheduleSettings>
      <AggressiveMode>0</AggressiveMode>
      <ScheduleType>0</ScheduleType>
      <ScheduledDay>0</ScheduledDay>
      <ScheduledDay>0</ScheduledDay>
      <ScheduledDay>0</ScheduledDay>
      <ScheduledDay>0</ScheduledDay>
      <ScheduledDay>0</ScheduledDay>
      <ScheduledDay>0</ScheduledDay>
      <ScheduledDay>0</ScheduledDay>
      <WindowStart>0</WindowStart>
      <WindowEnd>0</WindowEnd>
      <ScheduleInterval>0</ScheduleInterval>
    </BackupScheduleSettings>
    
    0 讨论(0)
  • 2020-12-31 09:44

    Decorate your property:

    [XmlElement("ScheduledDay")]
    public int[] ScheduledDay { get; set; }
    
    0 讨论(0)
提交回复
热议问题