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

  • Home
  • SEARCH
  • 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 5930357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:32:40+00:00 2026-05-22T14:32:40+00:00

Let’s say that I have two classes, a base class and a derived class.

  • 0

Let’s say that I have two classes, a base class and a derived class. They are fairly simple classes, and mainly just act as data structures (with the derived class obviously being slightly more complex).

public class BaseUserSession
{
    // ... Various properties ...
}

public class DerivedUserSession : BaseUserSession
{
    // ... Even more properties ...
}

These classes need to be serialized into XML. Now, this can be done simply by specifying [Serializable] above the class declarations, and the default XML serializer works well.

In reality, however, there are multiple classes that inherit from the base class. My goal is to be able to have the serialized XML of any of the derived classes be consumable by the base class.

I have tried two methods, neither of which work currently. First, using the native XML serializer and some control attributes within the base class. The generated XML looks something like this:

<?xml version="1.0" encoding="utf-16"?>
<DerivedUserSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SSO="www.mywebsite.com">
    <SSO:Id />
    <SSO:Authenticated>false</SSO:Authenticated>
    <SSO:Role />
    <EmailAddress />
    <FullName />
    <Street />
    <City />
    <State />
    <Zip />
    <DayPhone />
    <EveningPhone />
    <ErrorMessage />
    <Birthdate>0001-01-01T00:00:00</Birthdate>
    <CurrentControl>0</CurrentControl>
</DerivedUserSession>

The tags that are prefixed with SSO are the ones that were inherited from the base class. But when I attempt to deserialize this into a BaseUserSession object, it throws an exception: System.InvalidOperationException: <DerivedUserSession xmlns=''> was not expected.

I also tried making the base class IXmlSerializable, and reading and writing all of the XML manually. However, the derived classes fail to write out their properties, and I really don’t want to implement IXmlSerializable for every class.

Is there an easier way to do this?

=Edit=

I have finally implemented a fairly ugly method to accomplish this. The two classes inherit from the base, and they all implement IXmlSerializable. Each class (including the base) writes out its field values (painstakingly…) into its own XML hierarchy using reflection, as well as reads those values back. The first values written are always from the base class.

Each application also includes code that manages the serialization into XML and back, providing an easy interface. The session itself doesn’t do anything more now than simply accept the serialized XML and store it under a common key. The serialization into and from XML is all handled by the application, and a common XmlRootAttribute is used in all applications to bypass the InvalidOperationException.

So now, when another application tries to deserialize XML that was initially created in a different application, it manages to read through the common base class values before it hits an unrecognized element, at which point it goes and fetches the needed values elsewhere, which is possible because it will already have the common base values.

I need to read up on serialization principles…

  • 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-22T14:32:41+00:00Added an answer on May 22, 2026 at 2:32 pm

    XmlSerialization is not polymorphic (the member attributes, however, go with the declaring class; i.e. unless you overrule a member with the new keyword (in contrast to override) you’ll ‘inherit’ the serialization attributes.

    Deserialization always results in the actual type that was serialized. This makes a lot of sense, really.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say I have a simple Login servlet that checks the passed name and
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have some content classes like Page, TabGroup, Tab, etc. Certain of
Let's say I have some text as follows: do this, do that, then this,
Let's say I have the following entity: public class Store { public List<Product> Products

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.