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 7645719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:00:47+00:00 2026-05-31T10:00:47+00:00

I am trying to deserialize some xml into an IList, but I am having

  • 0

I am trying to deserialize some xml into an IList, but I am having problems. This is what I have done so far:

The XML:

<?xml version="1.0" encoding="utf-8"?>

<Animals>
    <Animal>
        <Name>Cow</Name>
        <Color>Brown</Color>
    </Animal>
</Animals>

The Model:

[XmlRoot("Animals")]
public class Model
{
    [XmlElement("Animal")]
    public IList<Animal> AnimalList { get; set; }
}

public class Animal
{
    [XmlElement("Name")]
    public string Name{ get; set; }
    [XmlElement("Color")]
    public string Color{ get; set; }
}

Deserialization:

FileStream fs = new FileStream("file.xml", FileMode.Open);
XmlReader xml = XmlReader.Create(fs);

XmlSerializer ser = new XmlSerializer(typeof(List<Model>));

var list = (List<Model>)ser.Deserialize(xml);

I get an invalid operation exception when running the code above. What am I doing wrong?

Thanks,
James Ford

  • 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-31T10:00:48+00:00Added an answer on May 31, 2026 at 10:00 am

    The problem is that you are using an IList<Animal>. You need to use a List<Animal> so that it knows the specific type to use.

    EDIT: Using the following code in LINQPad works perfectly. Only difference is I am loading the XML via string instead of file, but even when I change to a file it works fine. I just added the using for System.Xml.Serialization.

    void Main()
    {
        string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
        <Animals>
            <Animal>
                <Name>Cow</Name>
                <Color>Brown</Color>
            </Animal>
        </Animals>";
    
        XmlReader reader = XmlReader.Create(new StringReader(xml));
    
        XmlSerializer ser = new XmlSerializer(typeof(Model));
    
        var list = (Model)ser.Deserialize(reader);
        list.Dump();
    }
    
    // Define other methods and classes here
    [XmlRoot("Animals")]
    public class Model
    {
        [XmlElement("Animal")]
        public List<Animal> AnimalList { get; set; }
    }
    
    public class Animal
    {
        [XmlElement("Name")]
        public string Name{ get; set; }
        [XmlElement("Color")]
        public string Color{ get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to deserialize this xml: <?xml version=1.0 encoding=UTF-8?> <query xmlns:yahoo=http://www.yahooapis.com/v1/base.rng yahoo:count=10 yahoo:created=2012-02-20T19:07:02Z
I'm trying to deserialize some XML from a web service into C# POCOs. I've
I have some XML that I am trying to deserialize. The Kronos_WCF object deserializes
I have some XML that I am trying to deserialize the xml below. <?xml
I'm trying to deserialize the following XML document: <?xml version=1.0 encoding=utf-8 ?> <TestPrice> <Price>
I am having real trouble trying to deserialize some XML and was hoping someone
I have some xml files and I am trying to deserialize as below in
I'm trying to deserialize XML into C# objects. When I run my code, no
I am trying to deserialize this JSON array into my android project. [{Name:Ban,Price:1},{Name:Banana,Price:1},{Name:chicken,Price:14},{Name:pizza,Price:16},{Name:slice,Price:1}] I
I'm trying to deserialize an XML sitemap and then load it into a Datagrid

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.