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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:31:48+00:00 2026-06-18T23:31:48+00:00

I have an object I’m trying to serialize into XML. Inside this object is

  • 0

I have an object I’m trying to serialize into XML. Inside this object is a list of a generic type (abstract class). Each item in this list could be a different class, but all are inheriting from the abstract base class:

public abstract class animal
{
  public string type { get; set; }
}
public class cat:animal
{
  public string size { get; set; }
  public string furColor { get; set; }
}
public class fish:animal
{
  public string size { get; set; }
  public string scaleColor { get; set; }
}

When I serialize the list, I want it to look like this:

<animal type="cat">
  <size>medium</size>
  <furColor>black</furColor>
</animal>
<animal type="fish">
  <size>small</size>
  <scaleColor>silver</scaleColor>
</animal>

I’ve tried the simple solution:

[XmlElement("Animal")]
public List<animal> Animals { get; set; }

But it throws an error because it’s not expecting the object type “cat.” Adding the [XmlInclude] tag to either the base class, the derived class, or the whole containing class (let’s call it zoo) does not help this.

I can use the typeof designation for a single class:

[XmlElement("Animal", typeof(cat))]
public List<animal> Animals { get; set; }

and this works properly, as I want it to, as long as I only use cats. Again, the minute I add a fish to the mix, it blows up with the same error (not expecting fish).

I can add multiple typeof attributes:

[XmlElement("Animal")]
[XmlElementAttribute(typeof(cat))]
[XmlElementAttribute(typeof(fish))]
public List<animal> Animals { get; set; }

and this compiles, but ignores the element name, and serializes the objects as <cat> </cat> and <fish> </fish>, respectively, which is unacceptable.

I’ve even tried adding multiple [XmlElement] tags:

[XmlElement("Animal", typeof(cat))]
[XmlElement("Animal", typeof(fish))]
public List<animal> Animals { get; set; }

This one throws a different exception, this time that the objects “cat” and “fish” both use the type “Animal” in the same scope.

Can anyone think of a way around this?

UPDATE After a little more digging, I found This SO post which suggests adding the namespace to the base class:

[XmlRoot(Namespace="myNamespace")]
[XmlInclude(typeof(cat))]
[XmlInclude(typeof(fish))]
public abstract class animal

Serializing this yields the following:

<animal xsi:type="cat" type="cat">
  ...
</animal>
<animal xsi:type="fish" type="fish">
  ...
</animal>

Where xsi:type=”cat” refers to the name of the class, and type=”cat” refers to the type attribute created within the base class (see the very top example). This is so close to what I need, and I fear I may just be suffering from inexperience here, but is there a way to get rid of the xsi:type attribute listing?

  • 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-06-18T23:31:49+00:00Added an answer on June 18, 2026 at 11:31 pm

    Adding the XmlInclude Attribute to your base class should solve this serilazation issue

        [Serializable]
        [XmlInclude(typeof(cat))]
        [XmlInclude(typeof(fish))]
        class animals
        {
             ....
        }
    
        public class cat:animals
        {
          public string size { get; set; }
          public string furColor { get; set; }
        }
    
        public class fish:animals
        {
          public string size { get; set; }
          public string scaleColor { get; set; }
        }
    

    UPDATE

    Here’s a brief example that works on my side, you don’t actually need the type attribute to my opinion because you can retrieve it with typeof() method. unless the type attribute had another purpose.

            List<animal> animals = new List<animal>();
            cat catofdoom = new cat();
            catofdoom.furColor = "brown";
            catofdoom.size = "10 pounds";
            animals.Add(catofdoom);
    
            fish fishofdoom = new fish();
            fishofdoom.scaleColor = "blue";
            fishofdoom.size = "12 inches";
            animals.Add(fishofdoom);
    
    
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(List<animal>));
                using (StreamWriter wr = new StreamWriter("animal.xml"))
                {
                    xs.Serialize(wr, animals);
                }
            }
            catch (Exception e)
            {
    
                throw;
            }
    

    results into this ( very basic serialization with no options ) :

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfAnimal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <animal xsi:type="cat">
        <size>10 pounds</size>
        <furColor>brown</furColor>
      </animal>
      <animal xsi:type="fish">
        <size>12 inches</size>
        <scaleColor>blue</scaleColor>
      </animal>
    </ArrayOfAnimal>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have object XML serialized messages coming into a class called MessageRouter . The
I'm currently retrieving data using XML-RPC, this is what I have: Object[] params =
I have this object: stdClass Object ( [daily_inventoryID] => 1 [inventory_timestamp] => 2012-06-08 14:35:42
Does does class/object models have a out-of-the-box equivalent to a database foreign key constraint?
Let's imagine that we have object Animal $.Animal = function(options) { this.defaults = {
Lets say I have object Tom which has class Person. Class Person String Name
Say I have Object A which has a member of type Object B.. and
Let's say I have: object A, B, C each with corresponding models, views, and
i have object like this: some_object this object has like 1000 properties. i would
Can I have object with the same name as class in javascript?

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.