5

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?

5
  • Have you tried taking out all the "XmlElement" and "XmlRoot" attributes? I find serialization works best when I leave things alone as much as possible and let the framework sort it out for me. Commented Aug 11, 2011 at 20:44
  • It won't even deserialize if I take them out. Commented Aug 11, 2011 at 20:45
  • Try this suggestion: stackoverflow.com/questions/534451/… Commented Aug 11, 2011 at 20:47
  • Each Device element has to explicitly be called Device. Device could be an unlimited amount of elements within the Devices element. The suggestion above won't work in my case. Commented Aug 11, 2011 at 20:51
  • 1
    Good luck ! Hope you figure it out. Commented Aug 11, 2011 at 20:53

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Then maybe my issue is up the ladder, I set the deserializer typeof to a File class with this inside public sealed class File { [XmlElement("File")] public Devices Devices { get; set; } } Could this be my issue?
@bl4kh4k: Hard to say w/o seeing a complete code sample that reproduces the problem - would be good to update the question
Just updated with the missing class. If you can figure it out your a serializedGod.
I removed the XmlRootAttribute but it still will only deserialize the first Device out of the array.
It works, had some elements inside elements that weren't deserializing which apparently keeps it from adding to the array and breaks out. Thanks for your help. Cheers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.