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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:54:42+00:00 2026-05-13T09:54:42+00:00

I’ve got an object that is being serialized / deserialized via the XmlSerializer in

  • 0

I’ve got an object that is being serialized / deserialized via the XmlSerializer in C#, .NET 3.5. One of the properties (and more in the future) is a collection: List where T is an enum value. This serializes / deserializes fine.

We are also using a “default values” mechanism to provide default values for the object, in case the serialized version doesn’t have any value set. as a simple example, here is what we are dong:

public enum MyEnum {
  Value1,
  Value2
}

public class Foo
{

  public List SomeSetting{ get; set; }

  public Foo()
  {
    SomeSetting = new List();
    SomeSetting.Add(MyEnum.Value1);
    SomeSetting.Add(MyEnum.Value2);
  }
}

This code works fine for setting the default values of SomeSetting when the object is constructed.

However, when we are deserializing an xml file that has values for SomeSetting, this default value setup is causing problems: the xml deserializer does not ‘reset’ the SomeSetting collection – it does not wipe it clean and populate with new data. Rather, it adds on to the data that is already there. So, if the xml file has Value1 serialized into it, when I deserialize that file, i end up with SomeSettings having {Value1, Value2, Value1} as the values being stored.

I need a way for the xml deserialization process to allow my default values to exist when there is no data for SomeSetting in the xml document, and also to wholesale replace the SomeSetting values when there is data in the xml document. How can I do this?

FYI – this is not the only property in the document. The document does exist, and is being serialized / deserialized for the other ‘simple’ values. This is the property that is causing problems, though. I have to support this scenario because I need to do this a lot, now.

  • 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-13T09:54:43+00:00Added an answer on May 13, 2026 at 9:54 am

    FYI – i solved this with the IXMLSerializable interface. Note that this code is very specific to my needs in this one class, so YMMV.

    
    
            public void WriteXml(XmlWriter writer)
            {
                foreach (PropertyInfo prop in GetType().GetProperties())
                {
                    XmlIgnoreAttribute attr;
                    if (prop.TryGetAttribute(out attr))
                        continue;
    
                    if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition().Equals(typeof(List)))
                    {
                        XmlSerializer serializer = new XmlSerializer(prop.PropertyType, new XmlRootAttribute(prop.Name));
                        serializer.Serialize(writer, prop.GetValue(this, null));
                    }
                    else
                    {
                        writer.WriteElementString(prop.Name, prop.GetValue(this, null).ToString());
                    }
                }
            }
    
            public void ReadXml(XmlReader reader)
            {
                if (reader.IsEmptyElement)
                    return;
    
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(reader);
    
                Type type = GetType();
    
                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    PropertyInfo prop = type.GetProperty(node.Name);
                    if (prop != null && prop.CanWrite)
                    {
                        object value;
                        if (prop.PropertyType.IsEnum)
                        {
                            string stringValue = node.InnerText;
                            value = Enum.Parse(prop.PropertyType, stringValue);
                        }
                        else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition().Equals(typeof(List)))
                        {
                            Type enumType = prop.PropertyType.GetGenericArguments()[0];
                            value = Activator.CreateInstance(prop.PropertyType);
                            var addMethod = value.GetType().GetMethod("Add");
                            foreach (XmlNode subNode in node.ChildNodes)
                            {
                                object enumValue = Enum.Parse(enumType, subNode.InnerText);
                                addMethod.Invoke(value, new[] { enumValue });
                            }
                        }
                        else
                        {
                            string stringValue = node.InnerText;
                            value = Convert.ChangeType(stringValue, prop.PropertyType);
                        }
                        prop.SetValue(this, value, null);
                    }
                }
            }
    
            public XmlSchema GetSchema()
            {
                return null;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 290k
  • Answers 290k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The basic idea is fine. The implementation could be improved.… May 13, 2026 at 5:43 pm
  • Editorial Team
    Editorial Team added an answer You could write a custom authentication provider to Sql Server… May 13, 2026 at 5:43 pm
  • Editorial Team
    Editorial Team added an answer I agree with Louis, what you are doing is a… May 13, 2026 at 5:43 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.