c# - XML 序列化程序无法处理属性

标签 c# properties xml-serialization

在我正在处理的代码中,有一些属性被序列化。据我所知,所有这些都工作得很好,但只有一个。代码如下:

    // 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;
    }

序列化后,这就是我最终得到的结果:

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

AlertColors 属性有什么问题?为什么没有连载?

编辑:我已相应地更改了 AlertColors 属性,但它仍然不起作用:

    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;
                }

            }
        }
    }

我在 .xml 文件中得到的是这样的:

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

这是初始化 _colorArgb 字段时设置的第一个值。我可以在程序执行过程中看到字段发生变化,但是当覆盖属性被序列化时,它会使用底层字段的初始值进行序列化。

是什么导致了这个问题?

最佳答案

XMLSerializer 在序列化时仅使用类的公共(public)属性 - Color 类没有。

这是一个如何解决这个问题的好例子:http://www.codeproject.com/Articles/2309/NET-XML-Serialization-a-settings-class

关于c# - XML 序列化程序无法处理属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044886/

相关文章:

c# - 我可以将多个值传递给 C# 中的 Setter 吗?

c# - 使用 XmlSerializer 序列化 List<>

c# - 删除 k__BackingField XML 而不删除 [Serializable] 属性

c# - 何时使用ShouldSerializeXXX和XmlIgnoreAttribute进行XML序列化

小波中的 C# 音频指纹

C# 调用非托管 C 驱动程序 (dll)

grails - Grails 3. *-创建自定义属性文件并读取数据

ios - didSelectRowAtIndexPath 中的 @property NSArray

c# - 是否可以编写一个创建方法的方法?

c# - 如何从 OnResultExecuting 设置子模型内容?