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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:13:44+00:00 2026-05-18T10:13:44+00:00

The title may be long, let me explain what I mean. I won’t give

  • 0

The title may be long, let me explain what I mean. I won’t give you the actual XML that I need to work with, but I’ll give one that demonstrates the issue I’m facing.

I have an XML that looks like this:

<root>
    <creatures>
        <creature type="mammal" name="lion">
            <sound>roarr</sound>
        </creature>
        <creature type="bird" name="parrot">
            <color>red</color>
        </creature>
    </creatures>
</root>

Imagine the following classes:
(Of course these are not my real classes either, but you get the point.)

public class Creature
{
    public string Name { get; set; }
}

public class MammalCreature : Creature
{
    public string Sound { get; set; }
}

public class BirdCreature : Creature
{
    public string Color { get; set; }
}

I would like to use XML Serialization with attributes in a manner that I want the serializer to distinguish between the types MammalCreature and BirdCreature by the type attribute.

I’ve found solutions that can do this by setting the xsi:type attribute to the type name I want, but I’d like to know if there is an actual solution that does this for my case.

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

    Not with just attributes, no. However, this hairy looking code can indeed read your XML properly into a Root class:

    [XmlRoot("root")]
    public class Root : IXmlSerializable
    {
        [XmlElement("creatures")]
        public Creatures Items { get; set; }
    
        /// <summary>
        /// This method is reserved and should not be used. When implementing
        /// the IXmlSerializable interface, you should return null (Nothing in
        /// Visual Basic) from this method, and instead, if specifying a custom
        /// schema is required, apply the
        /// <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/>
        /// to the class.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the
        /// XML representation of the object that is produced by the
        /// <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/>
        /// method and consumed by the
        /// <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/>
        /// method.
        /// </returns>
        public XmlSchema GetSchema()
        {
            return null;
        }
    
        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream
        /// from which the object is deserialized. </param>
        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement("root");
            reader.ReadStartElement("creatures");
    
            List<Creature> creatures = new List<Creature>();
    
            while ((reader.NodeType == XmlNodeType.Element)
                && (reader.Name == "creature"))
            {
                Creature creature;
                string type = reader.GetAttribute("type");
                string name = reader.GetAttribute("name");
    
                reader.ReadStartElement("creature");
    
                switch (type)
                {
                    case "mammal":
                        MammalCreature mammalCreature = new MammalCreature();
    
                        reader.ReadStartElement("sound");
                        mammalCreature.Sound = reader.ReadContentAsString();
                        reader.ReadEndElement();
                        creature = mammalCreature;
                        break;
                    case "bird":
                        BirdCreature birdCreature = new BirdCreature();
    
                        reader.ReadStartElement("color");
                        birdCreature.Color = reader.ReadContentAsString();
                        reader.ReadEndElement();
                        creature = birdCreature;
                        break;
                    default:
                        reader.Skip();
                        creature = new Creature();
                        break;
                }
    
                reader.ReadEndElement();
                creature.Name = name;
                creature.Type = type;
                creatures.Add(creature);
            }
    
            reader.ReadEndElement();
            this.Items = new Creatures();
            this.Items.Creature = creatures.ToArray();
            reader.ReadEndElement();
        }
    
        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream
        /// to which the object is serialized. </param>
        public void WriteXml(XmlWriter writer)
        {
            new XmlSerializer(typeof(Creatures)).Serialize(writer, this.Items);
        }
    }
    
    [XmlRoot("creatures")]
    public class Creatures
    {
        [XmlElement("creature")]
        public Creature[] Creature { get; set; }
    }
    
    [XmlInclude(typeof(MammalCreature))]
    [XmlInclude(typeof(BirdCreature))]
    public class Creature
    {
        [XmlAttribute("type")]
        public string Type { get; set; }
    
        [XmlAttribute("name")]
        public string Name { get; set; }
    }
    
    public class MammalCreature : Creature
    {
        [XmlElement("sound")]
        public string Sound { get; set; }
    }
    
    public class BirdCreature : Creature
    {
        [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

That title may need some work. Perhaps after reading this, you may be able
The title may have been confusing, but please let me explain: Currently when I
This title may seem strange, so let me try to explain what I'm trying
Ok Title may not be clear so let me explain. I have 2 classes,
The title may have said enough but I'll explain it anyway: My application gets
Title may be confusing, but here's explanation with code. Based on some conditions, I
The title may not seem very clear - was not sure how to explain
The title may be a bit ambiguous, but I couldn't think of a better
The title may be a little confusing, but I don't know how else to
The title may sound confusing but it actually isn't, I just didn't know how

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.