C# 如何将字典序列化为XML文件?
我正试图维护一个配置字典 这是我的抽象类C# 如何将字典序列化为XML文件?,c#,refactoring,C#,Refactoring,我正试图维护一个配置字典 这是我的抽象类 [Serializable] public abstract class Configuration { } 这是一个具体的类(目前,我只有这个类) 我有一个类,它包含一个配置级别的字典 第一个参数是配置的名称。当name=”“表示默认配置时 水平意味着难度。有三个级别:简单、中等和困难 第三个是配置 我计划将这些配置写入XML文件中。我想把它们序列化,但这给我带来了一个错误。我该怎么办?据我所知,实现IDictionary的类不能被Xml序列化。
[Serializable]
public abstract class Configuration
{
}
这是一个具体的类(目前,我只有这个类)
我有一个类,它包含一个配置级别的字典
- 第一个参数是配置的名称。当
name=”“
表示默认配置时李>
- 水平意味着难度。有三个级别:简单、中等和困难
- 第三个是配置
我计划将这些配置写入XML文件中。我想把它们序列化,但这给我带来了一个错误。我该怎么办?据我所知,实现IDictionary的类不能被Xml序列化。
试试这个,它对我起作用。你可以考虑的另一个选项是使用A。我可以用这种方法把你的字典连载起来
如果你走这条路,要记住的事情:
- 您必须添加不同的属性。具体来说,您需要对类型使用DataContract,对属性使用DataMember
- 您需要确保所有属性都有setter,即使setter最终是私有的
- 创建DataContractSerializer时,需要确保它知道所有类型。为它指定字典的类型会处理字符串、级别和配置,但不会处理BinaryProblemConfiguration。可以通过额外的构造函数重载提供额外的类型信息
例如:
var dict = new Dictionary<string,Dictionary<Levels,Configuration>> ();
var wtr = XmlWriter.Create (Console.Out);
var dcSerializer = new DataContractSerializer (dict.GetType (), new [] {typeof (BinaryProblemConfiguration)});
dcSerializer.WriteObject (wtr, dict);
var dict=newdictionary();
var wtr=XmlWriter.Create(Console.Out);
var dcSerializer=newDataContractSerializer(dict.GetType(),new[]{typeof(BinaryProblemConfiguration)});
dcSerializer.WriteObject(wtr,dict);
如果您执行了上述操作,您也可以在以后切换到更紧凑的JSON格式(当然,假设XML不是一项硬性要求)。Codereview用于查看工作代码。“我如何做X”问题属于堆栈溢出。
/// <summary>
/// The abstract level configuration allows descendent classes to configure themselves
/// </summary>
public abstract class LevelConfiguration
{
private Dictionary<string, Dictionary<Levels, Configuration>> _configurableLevels = new Dictionary<string, Dictionary<Levels, Configuration>>();
/// <summary>
/// Adds a configurable level.
/// </summary>
/// <param name="level">The level to add.</param>
/// <param name="problemConfiguration">The problem configuration.</param>
protected void AddConfigurableLevel(string name, Levels level, Configuration problemConfiguration)
{
if (!_configurableLevels.ContainsKey(name))
{
_configurableLevels.Add(name, new Dictionary<Levels, Configuration>());
}
_configurableLevels[name].Add(level, problemConfiguration);
}
/// <summary>
/// Returns all the configurable levels.
/// </summary>
/// <param name="level"></param>
protected void RemoveConfigurableLevel(string name, Levels level)
{
_configurableLevels[name].Remove(level);
}
/// <summary>
/// Returns all the configurable names.
/// </summary>
/// <returns></returns>
public IEnumerable<string> GetConfigurationNames()
{
return _configurableLevels.Keys;
}
/// <summary>
/// Returns all the configurable levels.
/// </summary>
/// <returns></returns>
public IEnumerable<Levels> GetConfigurationLevels(string name)
{
return _configurableLevels[name].Keys;
}
/// <summary>
/// Gets the problem configuration for the specified level
/// </summary>
/// <param name="level">The level.</param>
/// <returns></returns>
public Configuration GetProblemConfiguration(string name, Levels level)
{
return _configurableLevels[name][level];
}
}
public class AdditionLevelConfiguration : LevelConfiguration
{
public AdditionLevelConfiguration()
{
AddConfigurableLevel("", Levels.Easy, GetEasyLevelConfiguration());
AddConfigurableLevel("", Levels.Medium, GetMediumLevelConfiguration());
AddConfigurableLevel("", Levels.Hard, GetHardLevelConfiguration());
AddConfigurableLevel("config2", Levels.Easy, GetEasyLevelConfiguration());
AddConfigurableLevel("config2", Levels.Medium, GetMediumLevelConfiguration());
var configs = this.GetProblemConfiguration("config2", Levels.Medium);
var configs2 = this.GetProblemConfiguration("", Levels.Easy);
}
protected Configuration GetHardLevelConfiguration()
{
return new BinaryProblemConfiguration
{
MinValue = 100,
MaxValue = 1000,
};
}
protected Configuration GetMediumLevelConfiguration()
{
return new BinaryProblemConfiguration
{
MinValue = 10,
MaxValue = 100,
};
}
protected Configuration GetEasyLevelConfiguration()
{
return new BinaryProblemConfiguration
{
MinValue = 1,
MaxValue = 10,
};
}
}
var dict = new Dictionary<string,Dictionary<Levels,Configuration>> ();
var wtr = XmlWriter.Create (Console.Out);
var dcSerializer = new DataContractSerializer (dict.GetType (), new [] {typeof (BinaryProblemConfiguration)});
dcSerializer.WriteObject (wtr, dict);