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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:15:38+00:00 2026-05-14T14:15:38+00:00

I have a List which is populated with objects of various concrete types which

  • 0

I have a List which is populated with objects of various concrete types which subclass BaseType

I am using the WCF DataContractSerializer

<Children>
    <BaseType xmlns:d3p1="http://schemas.datacontract.org/2004/07/Tasks"
              i:type="d3p1:ConcreteTypeA"></BaseType>
    <BaseType xmlns:d3p1="http://schemas.datacontract.org/2004/07/Tasks"
              i:type="d3p1:ConcreteTypeB"></BaseType>
</Children>

Is there any way to get this to generate

<Children>
    <ConcreteTypeA/>
    <ConcreteTypeB/>
</Children>

?

The real goal is to let users generate some XML to load into memory, and the users are of a skill level that asking them for the original XML is not going to be successful.

  • 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-14T14:15:39+00:00Added an answer on May 14, 2026 at 2:15 pm

    DataContractSerializer is not designed to let you control the output. It’s designed to be fast, implicit, and easy to attribute a class with.

    What you want is the XmlSerializer. This gives you a lot more control over the XML output.

    Note that in my example below I specified a lot of things that could have been inferred from the property names but just to give you the sense that you can override them in the attributes. In fact I think this whole class would serialize just fine if all the attributes were removed and some KnownTypeAttributes were applied but I haven’t tested it. I don’t know if this will give you the exact XML you described (it will create a root element above Children) but hopefully this sets you in the right direction.

    Attributes That Control XML Serialization

    [XmlRoot(Namespace="")]
    public class MyClass {
    
        [XmlArray("Children")]
        [XmlArrayItem("ConcreteTypeA", typeof(ConcreteTypeA))]
        [XmlArrayItem("ConcreteTypeB", typeof(ConcreteTypeB))]
        public BaseType[] Children {
            get;
            set;
        }
    
    }
    
    public class BaseType {
    }
    
    public class ConcreteTypeA : BaseType {
    }
    
    public class ConcreteTypeB : BaseType {
    }
    

    EDIT: I just tested and it produces something very close to what you were seeking.

    void Main()
    {
    
        var mc = new MyClass();
        mc.Children = new BaseType[] {
            new ConcreteTypeA(),
            new ConcreteTypeB(),
            new ConcreteTypeA(),
            new ConcreteTypeB()
        };
    
        var serializer = new XmlSerializer(typeof(MyClass));
    
        using ( var str = new StringWriter() ) {
            serializer.Serialize(str, mc);
            str.ToString().Dump();
        }
    
    }
    

    …produces… (useless xmlns removed from the top)

    <MyClass>
      <Children>
        <ConcreteTypeA />
        <ConcreteTypeB />
        <ConcreteTypeA />
        <ConcreteTypeB />
      </Children>
    </MyClass>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select list which is being populated using the values from a
I have a list which will store Number objects. The list will be populated
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a List which contains a list of objects and I want to
I have a UserForm class which has a select list populated from a related
Basically I have a drop down list which is populated with text and values
I have a ListBox (AvailableDevicesListBox), which is populated using data-binding in Silverlight 3. The
I have a class which will store a list of objects and will also
I have a list populated with duplicate entries. I am using a hash to
I have a list which contains various docIds. When the information is to be

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.