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

  • Home
  • SEARCH
  • 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 6082769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:19:35+00:00 2026-05-23T11:19:35+00:00

I am currently having a problem with de-serialization from XML when using derived classes.

  • 0

I am currently having a problem with de-serialization from XML when using derived classes. The serialization works just fine, but doing the reverse results in an unexpected element exception.

Sample code:

public class Foo
{
   //Some Properties
}

public class Bar : Foo
{
    //more properties
}

public class Holder : IXmlSerializable
{
   public Foo SomeObject;

   public void WriteXML(XmlWriter writer)
   {
       var lizer = new XmlSerializer(SomeObject.GetType());
       lizer.Serialize(writer,SomeObject);
   }

   public void ReadXML(XmlReader reader)
   {
       reader.MoveToContent();
       reader.ReadStartElement();

       var lizer = new XmlSerializer(typeof(Foo), new Type[] { typeof(Bar) });

       SomeObject = (Foo)lizer.Deserialize(reader);
   }

If Holder.SomeObject is set to an instance of Bar the serialization works exactly as expected. However the de-serialization is throwing. It was my understanding that if I gave the XmlSerializer ctor all of the possible types that it should be pick the correct one.

Is this not the case or am i just missing something? Thanks

  • 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-23T11:19:35+00:00Added an answer on May 23, 2026 at 11:19 am

    You need to use the same constructor for the XmlSerializer when you serialize as when you are deserializing, i.e. your code would change to this:

    public void WriteXML(XmlWriter writer)
    {
        var lizer = new XmlSerializer(typeof(Foo), new Type[] { typeof(Bar) });
        lizer.Serialize(writer,SomeObject);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently having the problem that when I use the login_required decorator from
I'm currently having a problem. I need to update Table A from Table B
I'm currently having a problem with a ShoppingCart for my customer. He wants to
I'm trying to build firefox but I'm having some problems. I currently have Visual
I'm having a bit of a problem using FieldOffset correctly with arrays. The code
I'm currently having a problem with what seems like the Timer_Tick event interfering with
I am currently having the problem that I have a (partial) program that is
I am currently having a problem with multiple div's toggling at the same time.
I'm currently having a problem passing messages between a server and client. As far
I am currently having difficulty using the Google weather API in JavaScript using Mootools.

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.