Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:22:43+00:00 2026-05-24T12:22:43+00:00

So, deserializing is working however I have some xml like this: <File> <Stuff>stuff</Stuff> <Devices>

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:22:44+00:00Added an answer on May 24, 2026 at 12:22 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML that I am consuming and deserializing. <Foo> <Bars Baz=9> <Bar>...</Bar>
I'm having trouble deserializing my XML file from a MemoryStream. I have a generated
I am currently deserializing JSON using XStream, and it has been working great. However,
I'm working on a project that involves some home-made (de-)serialization code, and I have
Having problems deserializing some xml into an object in C#. The error that I
I have some JSON that I am deserializing using Gson. { resp: { posts:
I'm trying to catch a ClassCastException when deserializing an object from xml. So, try
I have a HashMap that I am serializing and deserializing to an Oracle db,
I'm deserializing a custom object from a file to an object in my app
I'm saving a huge list of objects to a file, and later deserializing them.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.