我的格式为
<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>
我需要对其进行反序列化,更改其内容并将其保存回来。我在阅读ScheduledDay元素时遇到问题。 我的课就像
public class BackupScheduleSettings
{
public BackupScheduleSettings()
{
ScheduledDay = new int[7];
}
.....
public int[] ScheduledDay { get; set; }
.....
}
现在,当我加载具有ScheduledDay正确值的XML内容时,我的类数组仍为NULL。
我无法修改XML的内容/格式,因为它是遗留代码。我不想使用XDocument读取值,因为它是一个大型XML,我需要再次序列化它。
我在没有任何帮助的情况下搜索了很多。任何想法都将受到高度赞赏。
...谢谢
答案 0 :(得分:17)
您不希望XmlArrayItem
。您希望在没有父元素的情况下序列化整数数组,这意味着您应该使用XmlElement
来装饰数组本身。因为您有特定的订单,所以您需要在XmlElement属性上使用Order
值。这是类,相应地修改:
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;
}
这是生成的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>
答案 1 :(得分:15)
装饰你的财产:
[XmlElement("ScheduledDay")]
public int[] ScheduledDay { get; set; }
答案 2 :(得分:2)
您只需执行以下操作即可:
[XmlElement]
public int[] ScheduledDay { get; set; }
通过添加此属性,每次(de)序列化程序看到ScheduledDay元素时,它都会知道将它添加到此数组中。