Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize XML Array of Elements

Tags:

c#

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?

like image 258
bl4kh4k Avatar asked Aug 11 '11 20:08

bl4kh4k


1 Answers

You must be doing something wrong at another place. Just using the Devices part of your XML:

  <Devices>
    <Device Type="1">
      <Number>1</Number>
    </Device>
    <Device Type="2">
      <Number>2</Number>
    </Device>
  </Devices>

I was able to successfully deserialize a Devices class instance with multiple DeviceElements children given your classes above using similar code as you used in your last question:

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);

XmlSerializer deserializer = new XmlSerializer(typeof(Devices));
var devicesResult = (Devices)deserializer.Deserialize(reader);

Edit:

Using the full XML:

<File>
  <Stuff>stuff</Stuff>
  <Devices>
    <Device Type="1">
      <Number>1</Number>
    </Device>
    <Device Type="2">
      <Number>2</Number>
    </Device>
  </Devices>
</File>

This successfully deserializes for me:

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "File";
xRoot.IsNullable = true;

XmlSerializer deserializer = new XmlSerializer(typeof(File), xRoot);
var fileResult = (File)deserializer.Deserialize(reader);

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:

FileStream stream = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
XmlSerializer deserializer = new XmlSerializer(typeof(File));
var fileResult = (File)deserializer.Deserialize(reader);
like image 79
BrokenGlass Avatar answered Sep 30 '22 16:09

BrokenGlass