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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:06:29+00:00 2026-06-13T11:06:29+00:00

In the code that I’m working on, there are some properties that are serialized.

  • 0

In the code that I’m working on, there are some properties that are serialized. All of them works fine as far as I can see, but one. Here’s the code:

    // Fields that the properties use.
    private bool _useAlertColors = false;
    private List<Int32> _colorArgb = new List<Int32>(new Int32[]{Color.White.ToArgb(), Color.Yellow.ToArgb(), Color.Red.ToArgb()});

    // The one that does not work.
    public List<Color> AlertColors
    {
        get
        {
            List<Color> alertColors = new List<Color>();

            foreach (Int32 colorValue in _colorArgb)
            {
                alertColors.Add(Color.FromArgb(colorValue));
            }

            return alertColors;
        }

        set
        {
            // If there's any difference in colors
            // then add the new color
            for (int i = 0; i < _colorArgb.Count; i++)
            {
                if (_colorArgb[i] != value[i].ToArgb())
                {
                    _colorArgb[i] = value[i].ToArgb();
                    HasChanged = true;
                }

            }
        }
    }

    // One of those that work.
    public bool UseAlertColors
    {
        get
        {
            return _useAlertColors;
        }

        set
        {
            if (_useAlertColors != value)
            {
                _useAlertColors = value;
                HasChanged = true;
            }
        }
    }

    // Serializing method.
    public bool Serialize(string filePath)
    {
        if (string.IsNullOrEmpty(filePath))
        {
            Logger.Log("Can't save settings, empty or null file path.");

            return false;
        }

        FileStream fileStream = null;

        try
        {
            fileStream = new FileStream(filePath, FileMode.Create);
            FilePath = filePath;
            System.Xml.Serialization.XmlSerializerFactory xmlSerializerFactory =
                new XmlSerializerFactory();
            System.Xml.Serialization.XmlSerializer xmlSerializer =
                xmlSerializerFactory.CreateSerializer(typeof(Settings));

            xmlSerializer.Serialize(fileStream, this);

            Logger.Log("Settings have been saved successfully to the file " + filePath);
        }
        catch (ArgumentException argumentException)
        {
            Logger.Log("Error while saving the settings. " + argumentException.Message);

            return false;
        }
        catch (IOException iOException)
        {
            Logger.Log("Error while saving the settings. " + iOException.Message);

            return false;
        }
        finally
        {
            if (fileStream != null)
                fileStream.Close();
        }

        return true;
    }

After the serialization, this is what I end up with:

  <UseAlertColors>true</UseAlertColors>
  <AlertColors>
    <Color />
    <Color />
    <Color />
  </AlertColors>

What is wrong with the AlertColors property? Why is it not serialized?

Edit: I’ve changed the AlertColors property accordingly and it still is not working:

    public List<int> AlertColors
    {
        get
        {
            return _colorArgb;
        }

        set
        {
            // If there's any difference in colors
            // then add the new color
            for (int i = 0; i < _colorArgb.Count; i++)
            {
                if (_colorArgb[i] != value[i])
                {
                    _colorArgb[i] = value[i];
                    HasChanged = true;
                }

            }
        }
    }

What I get in the .xml file is this:

  <AlertColors>
    <int>-1</int>
    <int>-8355840</int>
    <int>-65536</int>
  </AlertColors>

Which are the very first values that were set when the _colorArgb field was initialized. I can see the field change during the program execution, but when the covering property is serialized, it is serialized with the initial value of the underlying field.

What is causing the problem?

  • 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-13T11:06:30+00:00Added an answer on June 13, 2026 at 11:06 am

    XMLSerializer only uses public properties of a class when serializing – The Color Class has none.

    Heres a good example of how to get around it: http://www.codeproject.com/Articles/2309/NET-XML-Serialization-a-settings-class

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

Sidebar

Related Questions

Code that had been working is suddenly not. Printing works fine in all browsers
I have some code that works with a two-dimensional grid (array) and I ended
Code that was working fine last week is suddenly throwing this exception: System.Data.SqlClient.SqlException (0x80131904):
The code: // That works pretty well curl -d user%5Blogin%5D=some%40email.pl&user%5Bpass%5D=testpass&user%5Bmemory%5D=on&user%5Bsubmit%5D=Login -L -c cookie.txt http://turbobit.net/user/login
The code that I am working on is changing my temporary variables, and I
The code that I am working with has tons of style that I want
Some code that I don't have control over is overriding the global JSON object
I have code that generates a List<string[]> variable but can't quite figure out how
I have some code that will change the background color of a specific label
I have written some code that loads an XML document using an XmlDocument object

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.