DataContractSerializer DeSerialize枚举词典(和双精度)

时间:2014-02-10 17:11:44

标签: c# serialization dictionary enums datacontractserializer

我正在尝试将XML DeSerialize为对象。 object和xml如下所示:

    <?xml version="1.0" encoding="utf-16"?>
<SystemData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07">
<SystemBaseMVA>100</SystemBaseMVA>
<Voltage xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfVariantdoubleM9TNTpwj>
        <d2p1:Key>Rated</d2p1:Key>
        <d2p1:Value>400</d2p1:Value>
    </d2p1:KeyValueOfVariantdoubleM9TNTpwj>
    <d2p1:KeyValueOfVariantdoubleM9TNTpwj>
        <d2p1:Key>Actual</d2p1:Key>
        <d2p1:Value>400</d2p1:Value>
    </d2p1:KeyValueOfVariantdoubleM9TNTpwj>
</Voltage>
</SystemData>

对象......

[DataContract]
public class SystemData
{
    private readonly double _ratedVoltage;
    private readonly double _actualVoltage;

    #region ctor
    internal SystemData(
        double systemBaseMva,
        double ratedVoltage,
        double actualVoltage)
    {
        SystemBaseMVA = systemBaseMva;
        _ratedVoltage = ratedVoltage;
        _actualVoltage = actualVoltage;
    }
    #endregion

    #region Properties
    [DataMember]
    public double SystemBaseMVA { get; private set; }

    [DataMember]
    public Dictionary<Variant, double> Voltage
    {
        get
        {
            return new Dictionary<Variant, double>
            {
                {Variant.Rated, _ratedVoltage},
                {Variant.Actual, _actualVoltage}
            };
        }
    }
    #endregion

}

枚举:

[DataContract(Name = "Variant")]
public enum Variant : int
{
    [EnumMember]
    Rated = 1,
    [EnumMember]
    Actual = 2
}

但是当DeSerialize使用以下代码时,它会抛出“已经添加了具有相同键的项目。”有什么想法吗?

    public static T GetObject<T>(string xml) where T : class
    {
        T obj = null;
        using (var ms2 = new MemoryStream(GetBytes(xml)))
        {
            DataContractSerializer sr2 = new DataContractSerializer(typeof(T));
            obj = (T)sr2.ReadObject(ms2);
        }
        return obj;
    }

1 个答案:

答案 0 :(得分:1)

我从问题的陈旧性中猜测你已经解决了这个问题,但异常中的错误信息具有误导性:可以序列化和反序列化在枚举上键入的字典。问题是在SystemData类上没有字典属性的设置器,无论是私有还是公共。因此DataContractSerializer无法将信息保存回类中。添加私有的setter并使字段不是只读的工作:

public class SystemData
{
    private double _ratedVoltage;
    private double _actualVoltage;

    #region ctor
    internal SystemData(
        double systemBaseMva,
        double ratedVoltage,
        double actualVoltage)
    {
        SystemBaseMVA = systemBaseMva;
        _ratedVoltage = ratedVoltage;
        _actualVoltage = actualVoltage;
    }
    #endregion

    #region Properties
    [DataMember]
    public double SystemBaseMVA { get; private set; }

    [DataMember]
    public Dictionary<Variant, double> Voltage
    {
        get
        {
            return new Dictionary<Variant, double>
            {
                {Variant.Rated, _ratedVoltage},
                {Variant.Actual, _actualVoltage}
            };
        }
        private set
        {
            double itemValue;
            if (value.TryGetValue(Variant.Rated, out itemValue))
                _ratedVoltage = itemValue;
            if (value.TryGetValue(Variant.Actual, out itemValue))
                _actualVoltage = itemValue;
        }
    }
    #endregion
}