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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:12:19+00:00 2026-06-06T04:12:19+00:00

I have a data class that is serialized with the DataContractSerializer . The class

  • 0

I have a data class that is serialized with the DataContractSerializer. The class uses the [DataContract] attribute with no explicit Namespace declaration. As such, the namespace in the resulting xml file is generated based on the namespace of the class.

The class basically looks like this:

namespace XYZ
{
   [DataContract]
   public class Data
   {
      [DataMember(Order = 1)]
      public string Prop1 { get; set; }

      [DataMember(Order = 2)]
      public int Prop2 { get; set; }
   }
}

…and the resulting xml:

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XYZ">
   <Prop1>StringValue</Prop1>
   <Prop2>11</Prop2>
</Data>

Now I want to change the namespace of the class (actually remove it) by changing the [DataContract] attribute to [DataContract(Namespace = "")]. However, once I do this any file previously serialized with the original namespace with no longer deserialize. I receive the following exception:

Error in line 1 position XXX. Expecting element 'Data' from namespace ''.. Encountered 'Element' with name 'Data', namespace 'http://schemas.datacontract.org/2004/07/XYZ'.

This makes perfect sense. I changed the namespace. I’m ok with that. However, it seems like there must be a way to tell the DataContractSerializer to go ahead and deserialize that data even though the namespaces don’t match.

  • 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-06T04:12:21+00:00Added an answer on June 6, 2026 at 4:12 am

    One possible way is to wrap the reader used by the serializer in a reader which maps the old namespace to the new one, as shown below. A lot of code, but mostly trivial.

    public class StackOverflow_11092274
    {
        const string XML = @"<?xml version=""1.0"" encoding=""utf-8""?> 
    <Data xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/XYZ""> 
       <Prop1>StringValue</Prop1> 
       <Prop2>11</Prop2> 
    </Data>";
    
        [DataContract(Name = "Data", Namespace = "")]
        public class Data
        {
            [DataMember(Order = 1)]
            public string Prop1 { get; set; }
    
            [DataMember(Order = 2)]
            public int Prop2 { get; set; }
        }
    
        public class MyReader : XmlReader
        {
            XmlReader inner;
            public MyReader(XmlReader inner)
            {
                this.inner = inner;
            }
    
            public override int AttributeCount
            {
                get { return inner.AttributeCount; }
            }
    
            public override string BaseURI
            {
                get { return inner.BaseURI; }
            }
    
            public override void Close()
            {
                inner.Close();
            }
    
            public override int Depth
            {
                get { return inner.Depth; }
            }
    
            public override bool EOF
            {
                get { return inner.EOF; }
            }
    
            public override string GetAttribute(int i)
            {
                return inner.GetAttribute(i);
            }
    
            public override string GetAttribute(string name, string namespaceURI)
            {
                return inner.GetAttribute(name, namespaceURI);
            }
    
            public override string GetAttribute(string name)
            {
                return inner.GetAttribute(name);
            }
    
            public override bool IsEmptyElement
            {
                get { return inner.IsEmptyElement; }
            }
    
            public override string LocalName
            {
                get { return inner.LocalName; }
            }
    
            public override string LookupNamespace(string prefix)
            {
                return inner.LookupNamespace(prefix);
            }
    
            public override bool MoveToAttribute(string name, string ns)
            {
                return inner.MoveToAttribute(name, ns);
            }
    
            public override bool MoveToAttribute(string name)
            {
                return inner.MoveToAttribute(name);
            }
    
            public override bool MoveToElement()
            {
                return inner.MoveToElement();
            }
    
            public override bool MoveToFirstAttribute()
            {
                return inner.MoveToFirstAttribute();
            }
    
            public override bool MoveToNextAttribute()
            {
                return inner.MoveToNextAttribute();
            }
    
            public override XmlNameTable NameTable
            {
                get { return inner.NameTable; }
            }
    
            public override string NamespaceURI
            {
                get
                {
                    if (inner.NamespaceURI == "http://schemas.datacontract.org/2004/07/XYZ")
                    {
                        return "";
                    }
                    else
                    {
                        return inner.NamespaceURI;
                    }
                }
            }
    
            public override XmlNodeType NodeType
            {
                get { return inner.NodeType; }
            }
    
            public override string Prefix
            {
                get { return inner.Prefix; }
            }
    
            public override bool Read()
            {
                return inner.Read();
            }
    
            public override bool ReadAttributeValue()
            {
                return inner.ReadAttributeValue();
            }
    
            public override ReadState ReadState
            {
                get { return inner.ReadState; }
            }
    
            public override void ResolveEntity()
            {
                inner.ResolveEntity();
            }
    
            public override string Value
            {
                get { return inner.Value; }
            }
        }
    
        public static void Test()
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(Data));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
            try
            {
                XmlReader r = XmlReader.Create(ms);
                XmlReader my = new MyReader(r);
                Data d = (Data)dcs.ReadObject(my);
                Console.WriteLine("Data[Prop1={0},Prop2={1}]", d.Prop1, d.Prop2);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data access class that took me a while to get working.
If you have data for a class that will be modified and needs to
I have a very basic data class that is subclassed from NSObject. I declare
Suppose I have a class that processes some data: class SomeClass { public: void
In the data structures class that I am currently taking, we have been tasked
I have a class that contains data from some model. This class has metadata
I have a data object -- a custom class called Notification -- that exposes
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have a serializable Message class that has a Data As Object property that
I have create my own NSOpenGLView class, right now the data that i want

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.