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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:44:07+00:00 2026-06-11T10:44:07+00:00

Is there a native way to map the following xml… <Tag type=1 /> <Tag

  • 0

Is there a native way to map the following xml…

<Tag type="1" />
<Tag type="2" />

… to the concrete classes “Type1Tag” and “Type2Tag” (both deriving from abstract class Tag), based on the value of the “type” attribute?

(something similar to NHibernate’s discriminator: DiscriminateSubClassesOnColumn(…)/DiscriminatorValue(…) )

I’m not looking for something like the mapping reported in XmlSerializer + Polymorphism , which discriminates types by the tag name, instead of by an attribute value (I can’t change the xml structure) 🙂

  • 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-11T10:44:08+00:00Added an answer on June 11, 2026 at 10:44 am

    I’m probably too late in your implementation to be of any help, but this is something I figured out this weekend and thought I’d share for other people to find as it seems to meet my needs.

    Please note that there is no error handling here, and you can create your own child object type initialization depending on your needs.

    I have a number of different classes that are all of the same type of element, but could hold different content based on the value of an attribute.

    I used the IXmlSerializable-interface and implemented the method to read each type tag…

    Xml Input:

    <Parent>
      <Tag type="1"/>
      <Tag type="2"/>
    </Parent>
    

    The parent class is what implements IXmlSerializable:

    public class Parent : IXmlSerializable
    {
        [XmlElement("Tag")]
        public List<TagBase> Tags { get; set; }
    
        #region IXmlSerializable Members
    
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
    
        public void ReadXml(System.Xml.XmlReader reader)
        {
           // this line causes the root Parent node to be ignored, and gets to the children Tag elements
           reader.Read();
    
           while (reader.ReadToNextSibling("Tag"))
           {
               string attributeType = reader.GetAttribute("type");
    
               Type type = null;
    
               switch(attributeType)
               {
                  case "1":
                      type = typeof(Type1Tag);
                      break;
                  case "2":
                      type = typeof(Type2Tag);
                      break;
                  default:
                      continue;
               }
    
               XmlSerializer serializer = new XmlSerializer(type);
    
               // this line is the key to rejoining the automatic xml deserialization of all
               // the child stuff
               TagBase tagBase = (TagBase)serializer.Deserialize(reader.ReadSubtree());
    
               this.Tags.Add(tagBase);
            }
        }
    
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            throw new NotImplementedException();
        }
    
        #endregion IXmlSerializable Members
    }
    

    I created an abstract TagBase-class that each tag type would inherit:

    [XmlRoot("Tag")]
    public abstract class TagBase
    {
       [XmlAttribute("type")]
       public string Type { get; set; }
    }
    

    Then, each other class can be implemented normally with your custom properties, etc…

    [XmlRoot("Tag")]
    public class Type1Tag : TagBase
    {
        // your implementation
    }
    
    [XmlRoot("Tag")]
    public class Type2Tag : TagBase
    {
        // your implementation
    }
    

    Note, that I used XmlRootAttribute here and included them, as I had a bunch of namespaces that caused exceptions when trying to deserialize the child tags, but YMMV. I also haven’t gotten to the point of adding the WriteXml-method, but should be pretty straight forward here…

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

Sidebar

Related Questions

Is there a framework or a native way in .net to dynamically generate wrappers
Is there any way to achieve compiling Node.js scripts as native code, like Hip-Hop
Is there a way to communicate with cmd (using native process) in adobe air.
Is there a way to complie code directly into Native Code instead of MSIL
Is there a way to create windows like the native Windows 7 notification area
Is there a simple way to trigger a mobile OS's native pop-up/alert/etc. from some
From the native Win32 API using C++ is there a way to determine whether
Some questions about Android and Google Directions Service: Is there a native way on
Is there a native way to swipe/fling between different ActionBar tabs (the Honeycomb/ICS native
Is there a native way of escaping strings for Ormlite for Android? For example,

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.