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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:20:04+00:00 2026-05-29T07:20:04+00:00

I am using a child type with no properties in order to place fluent

  • 0

I am using a child type with no properties in order to place fluent validation rules on the object when it is being used in a particular way.

so

public class User
{
    public string name {get;set;}
}

public class myUser : User{}

However when the object gets to my serialiser I want it to serialise it to the base type. so ‘User’ is the root element not ‘myUser’.

  • 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-29T07:20:06+00:00Added an answer on May 29, 2026 at 7:20 am

    Why not simply cast it back to User before doing the serialization?

    After doing some digging around in the XmlSerializer, this does it for me:

    var myUser = new myUser();
    myUser.Name = "Test name";
    var serializer = new XmlSerializer(typeof(User), new Type[] {myUser.GetType()});
    serializer.Serialize(Console.Out, myUser);
    

    What is being done here is creating an XmlSerializer with the base type (User in this case) you want to cast into, as well as an array of other types that you want to allow. These types have to be derived from the base type, it would seem.

    Output:

    <?xml version="1.0" encoding="ibm850"?>
    <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org 2001/XMLSchema" xsi:type="myUser">
      <Name>Test name</Name>
    </User>
    

    Note the xsi:type="myUser", not sure if this will cause a problem for your deserialization process.

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

Sidebar

Related Questions

What is the use of creating base class object using child class reference in
I'm using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of
I want to create a tab using child tab having intents, so that when
Using parent child relationship where a parent can have children while each child has
How would I select all but the last child using CSS3 selectors? For example,
Why does every row have a red background when I'm using nth-child(odd) ? <div
I tried to implement the following in less: nav > ul > li:first-child using:
How can I do a multi-level parent-child sort using Linq if I have a
I'm trying to map a parent-child relationship using NHibernate (2.1.2), MySql.Data (6.2.2) and MySQL
I'm having a problem implementing a bi-directional parent/child relationship using hibernate 3. The parent,

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.