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

为君一笑 提交于 2019-11-29 12:16:51

问题


I have an XML in the form

 <BackupSchedule>
    <AggressiveMode>0</AggressiveMode>  
    <ScheduleType>0</ScheduleType>  
    <ScheduledDay>0</ScheduledDay>  
    <ScheduledDay>1</ScheduledDay>  
    <ScheduledDay>0</ScheduledDay>  
    <ScheduledDay>0</ScheduledDay>  
    <ScheduledDay>0</ScheduledDay>  
    <ScheduledDay>0</ScheduledDay>  
    <ScheduledDay>0</ScheduledDay>  
    <WindowStart>480</WindowStart>  
    <WindowEnd>1020</WindowEnd>  
    <ScheduleInterval>0</ScheduleInterval>  
  </BackupSchedule>

I need to deserialize it, change its contents and than save it back. I am facing problem in reading ScheduledDay element. My class is like

public class BackupScheduleSettings  
{  
        public BackupScheduleSettings()  
        {   
            ScheduledDay = new int[7];  
        }  

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

Now when I load XML content which has right values for ScheduledDay, my class array is still NULL.

I can't modify the content/format of XML since it is legacy code. I don't want to use XDocument to read the value since it is a large XML and I need to serialize it again.

I have searched a lot without any help. Any ideas will be highly appreciated.

Thanks...


回答1:


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>



回答2:


Decorate your property:

[XmlElement("ScheduledDay")]
public int[] ScheduledDay { get; set; }



回答3:


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.



来源:https://stackoverflow.com/questions/5259911/deserialize-multiple-xml-elements-with-the-same-name-through-xmlserializer-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!