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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:43:01+00:00 2026-05-28T03:43:01+00:00

Edit This code should illustrate the whole problem: [XmlInclude(typeof(AThing1))] public abstract class AThing {

  • 0

Edit This code should illustrate the whole problem:

[XmlInclude(typeof(AThing1))]
public abstract class AThing
{
    public abstract string Name { get; set; }
}

[XmlInclude(typeof(IThing1))]
public interface IThing
{
    string Name { get; set; }
}

public class AThing1 : AThing
{
    public override string Name { get; set; }
}

public class IThing1 : IThing
{
    public string Name { get; set; }
}

List<AThing> aThings = new List<AThing>(new AThing[] { new AThing1() { Name = "Bob" } });
List<IThing> iThings = new List<IThing>(new IThing[] { new IThing1() { Name = "Bob" } });

public void Test()
{
    using (StringWriter sw = new StringWriter())
    {
        XmlSerializer aSerializer = new XmlSerializer(typeof(List<AThing>));
        aSerializer.Serialize(sw, aThings);
        string text = sw.ToString();
    }

    using (StringWriter sw = new StringWriter())
    {
        // This line will throw "Cannot serialize interface IThing.":
        XmlSerializer iSerializer = new XmlSerializer(typeof(List<IThing>));    
        iSerializer.Serialize(sw, iThings);
        string text = sw.ToString();
    }
}

The first text generated by aSerializer will be:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfAThing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AThing xsi:type="AThing1">
    <Name>Bob</Name>
  </AThing>
</ArrayOfAThing>

I don’t see why can’t iSerializer do this:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfIThing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <IThing xsi:type="IThing1">
    <Name>Bob</Name>
  </IThing>
</ArrayOfIThing>

instead of throwing an exception.

  • 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-28T03:43:02+00:00Added an answer on May 28, 2026 at 3:43 am

    you can serialize interface, but not as simple as classes:

    http://ventspace.wordpress.com/2010/02/20/how-to-serialize-interfaces-in-net/

    but to answer your question I have 2 guesses on this:

    The first reason is from the practical side; The semantics of serializing an
    interface are a little bit blurry. What do you you think the serializer
    should serialize when you pass in an interface reference ? If you only
    serialize the interface properties your deserialize then could wind up with
    a half-way uninitialized object. There’s no telling what that would do to
    your application.

    If you serialize the full object together with the type information then
    serializing the interface really did not buy you anything. You could type
    the reference as a class type in the first place if your application really
    cares what object is there.

    The second one goes with the stated purpose of the XmlSerializer. Despite
    the misleading name XML Serialization in the .NET Framework really is a data
    binding technology with the primary intention to map MXL data types defined
    in XSD schemas to .NET types. The XSD definition knows about abstract base
    classes, but since it’s data centric, does not know anything about
    interfaces. With that in mind there is little motivation to support
    interfaces in the XmlSerializer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a StackOverflow Error in this code: EDIT [XmlAttribute(ID)] public string ID
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
(EDIT: This question is now outdated for my particular issue, as Google Code supports
Edit: I put this snippet of code in jsbin: http://jsbin.com/eneru I am trying to
I have this html code that i want to edit with jQuery. Here is
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating
Edit: Of course my real code doesn't look exactly like this. I tried to
how does one use code to do this: produce 15 random numbers [EDIT: from
I am trying to edit this code so that instead of just inserting the

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.