So, deserializing is working however I have some xml like this:
<File>
<Stuff>stuff</Stuff>
<Devices>
<Device Type="1">
<Number>1</Number>
</Device>
<Device Type="2">
<Number>2</Number>
</Device>
</Devices>
</File>
When it gets to the array of devices… the first device is the only one that is populated into an object. Here are the classes:
XmlSerializer deserializer;
XmlRootAttribute xRoot = new XmlRootAttribute();
FileStream stream = new FileStream(CONFIG_PATH, FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
// Details configuration area.
xRoot.ElementName = "File";
xRoot.IsNullable = true;
deserializer = new XmlSerializer(typeof(File), xRoot);
Configuration = (File)deserializer.Deserialize(reader);
[Serializable()]
[XmlRoot(ElementName = "File", IsNullable = true)]
public sealed class File
{
[XmlElement("Devices")]
public Devices Devices { get; set; }
}
// <summary>
/// Configuration device elements.
/// </summary>
[Serializable()]
[XmlRoot("Devices", IsNullable = true)]
public sealed class Devices
{
[XmlElement("Device")]
public DeviceElements[] DeviceElements { get; set; }
}
/// <summary>
/// Configuration Devices.
/// </summary>
[Serializable()]
[XmlRoot("Device", IsNullable = true)]
public sealed class DeviceElements
{
[XmlAttribute("Type")]
public string DeviceType { get; set; }
[XmlElement("Number")]
public int DeviceNumber { get; set; }
}
Again, only the first Device is populated, I’ve played with XmlArray And XmlArrayItem however neither of them were even giving me the first value. Any suggestions?
You must be doing something wrong at another place. Just using the
Devicespart of your XML:I was able to successfully deserialize a
Devicesclass instance with multipleDeviceElementschildren given your classes above using similar code as you used in your last question:Edit:
Using the full XML:
This successfully deserializes for me:
On a side note – you probably shouldn’t name your class the same as one already in the .NET framework (
System.IO.File), also in above code example setting the Xml root is not required since you use the same name anyway, just leave this out: