【问题标题】:XML Serializer not working on a propertyXML 序列化程序不适用于属性
【发布时间】:2012-10-24 16:19:58
【问题描述】:

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

    // 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 字段时设置的第一个值是哪些。在程序执行过程中我可以看到字段的变化,但是当覆盖属性被序列化时,它是用底层字段的初始值序列化的。

导致问题的原因是什么?

【问题讨论】:

    标签: c# properties xml-serialization


    【解决方案1】:

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

    这是一个很好的解决方法:http://www.codeproject.com/Articles/2309/NET-XML-Serialization-a-settings-class

    【讨论】:

    • 次要点:它还序列化公共字段,但这并没有改变这里的任何重要内容。你说得对,ColorXmlSerializer 不友好
    • 我更改了代码,但仍然无法正常工作。我已经指出了问题的变化。
    • 改用 DataContractSerializer。
    • 但是这段代码有什么问题呢?有int 属性,它们是序列化的。
    • 它工作得很好——你已经将它从 Color 更改为 int,现在它序列化一个 int——我对你的要求感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多