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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:40:44+00:00 2026-06-17T23:40:44+00:00

I got some issues with the XmlSerializer in .NET. Here I have a small

  • 0

I got some issues with the XmlSerializer in .NET.

Here I have a small example I built up just right now. (also available @ gist https://gist.github.com/2d84be9041a3f9c06237)

using System.IO;
using System.Xml.Serialization;

namespace XmlSerializingSample
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var specialType = new SpecialType()
                {
                    Id = 1,
                    Name = "test"
                };

            var serializer = new XmlSerializer(typeof (SpecialType));
            var des = new XmlSerializer(typeof (BaseType));
            using (var memeStream = new MemoryStream())
            {
                serializer.Serialize(memeStream, specialType);
                memeStream.Flush();

                memeStream.Seek(0, SeekOrigin.Begin);
                var instance = des.Deserialize(memeStream); // Here it throws the exception
            }
        }
    }

    [XmlInclude(typeof(SpecialType))]
    [XmlType("baseType")]
    public class BaseType
    {
        public long Id { get; set; }
    }

    [XmlRoot("special")]
    public class SpecialType : BaseType
    {
        public string Name { get; set; }
    }
}

In line 24 of the code I get an InvalidOperationException stating “{“<special xmlns=”> wurde nicht erwartet.”}” [yes, it’s german]

All Posts I found stated, after adding XmlIncludeAttribute on the base type being deserialized, this should work. Did I forget sth.?

Regards,
MacX

  • 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-17T23:40:46+00:00Added an answer on June 17, 2026 at 11:40 pm

    The problem is that your Serializer is serializing the SpecialType with a root element as so:

    <special ...>
       <Id>...
    

    But then you try to Deserialize it using var des = new XmlSerializer(typeof (BaseType)); it knows about both types but it doesn’t know how to handle the root element in the xml.

    If you want this to work, you need to also set the root element of the base type to serialize as special. In other words, you would need to do this:

    [XmlInclude(typeof(SpecialType))]
    [XmlType("baseType")]
    [XmlRoot("special")]
    public class BaseType
    {
          public long Id { get; set; }
    }
    

    That way, the deserializer knows how to handle special as the root element.

    I don’t think there are other simple alternatives to make this work out of the box.

    Update

    This is another alternative using the XmlAttributeOverrides class.

    LinqPad code:

    void Main()
    {
          var specialType = new SpecialType()
                    {
                        Id = 1,
                        Name = "test"
                    };
    
                var serializer = new XmlSerializer(typeof (SpecialType));
    
                XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
                XmlAttributes attrs = new XmlAttributes();
    
                // Create an XmlRootAttribute to override.
                XmlRootAttribute attr = new XmlRootAttribute();
                attr.ElementName = "special";
    
                // Add the XmlRootAttribute to the collection of objects.
                attrs.XmlRoot=attr; 
                attrOverrides.Add(typeof(BaseType),  attrs);
                var des = new XmlSerializer(typeof (BaseType), attrOverrides);
    
                using (var memeStream= new MemoryStream())
                {
                    serializer.Serialize(memeStream, specialType);
                    memeStream.Flush();
    
                    memeStream.Seek(0, SeekOrigin.Begin);
                    var instance = des.Deserialize(memeStream); 
                }
    
    }
    
    [XmlInclude(typeof(SpecialType))]
    [XmlType("baseType")]
    public class BaseType
    {
          public long Id { get; set; }
    }
    
    [XmlRoot("special")]
    public class SpecialType : BaseType
    {
            public string Name { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got some issues using the DAGLayout algorithm of JUNG and subsequently reading
I'm working on an ASP.NET MVC 3 application and having some issues. I've got
Having some issues deploying this. I've tried to deploy it twice now. Here's what
I've got some issues with a .htaccess that I just can't get to work.
I have got some issues when using fixed Header & Footer, But some suggest
I was testing my mobile application, with Windows mobile.I got some issues with the
I try to do static class, add to icollection but i got some issues
got some problems. Built an applet that has to be used step-by-step. After each
I got some pretty messy urls that i got via scraping here, problem is
I got some issues while trying to bind an ObservableCollection made from 2 others.

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.