我有不同类型成员的对象,如下所示:
public class MyObject
{
public string Str1 = string.Empty;
public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown;
public bool Done = false;
};
I have Dictionary
这些对象的:
Dictionary<string, MyObject> MyObjectsDic = new Dictionary<string, MyObject>();
和它的序列化器如下所示:
public static void ToXml(string file, string collectionName, Dictionary<string, object> collection)
{
XElement root = new XElement(collectionName);
root.Add(collection.Select(x => new XElement("Item", new XAttribute("Object", x.Key),
x.Value.GetType().GetFields().Select(f => new XElement(f.Name, f.GetValue(x.Value))))));
root.Save(file);
}
序列化器采用抽象Dictionary
作为参数,我需要转换我的MyObjectsDic
手动。也许我在这里弄错了。
ToXml("MyFile.xml", "MyObjects", MyObjectsDic.ToDictionary(p => p.Key, p => (object)p.Value));
I used 这个建议 https://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us制作序列化器。它工作得很好,但我需要添加到 MyObject 新成员
List<MyEnums.Enum2> Codes = new List<MyEnums.Enum2>();
并在这里存储一些值
var a = new MyObject {...};
a.Codes.Add(MyEnums.Enum2.Code1);
a.Codes.Add(MyEnums.Enum2.Code2);
MyObjectsDic.Add("Obj1", a);
但这些列表被序列化成文件,例如
<Codes>Code1Code2<Codes/>
没有任何空格或分隔符。而且我不知道如何在不修改序列化器且不添加新的奇怪代码的情况下使其更具可读性。我唯一的想法是将已经准备好的字符串保留在 MyObject 而不是 List<...> 中。它并不优雅,但简单且有效。我不读取这些数据,只是将其写入并保存为日志文件。
或者我需要改变我这么酷的序列化器?
Upd.
我使用了下面的解决方案,但我收到了例外在 Windows XP 上。在其他操作系统上它运行良好。我修改了代码,使其像助手而不是类扩展。
Exception during dumping MyObjectsDic: There was an error reflecting type 'MyObject'.
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at MyXmlSerializerHelper.SerializeToXElement[T](T obj, XmlSerializer serializer, Boolean omitStandardNamespaces) in MyXmlSerializerHelper.cs:line 16
at MyXmlSerializerHelper. <SerializeToFile>b__0[T](KeyValuePair'2 x) in MyXmlSerializerHelper.cs:line 5
我唯一的想法 - 不同版本的框架或 XP 上的一些其他宗教问题...不幸的是,我无法在生产中安装任何其他软件或 .Net 版本。