如何使用XElement在C#中序列化包含List的对象?

4

我有一个包含不同类型成员的对象,就像这样:

public class MyObject
{
    public string Str1 = string.Empty;
    public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown;
    public bool Done = false;
};

我有一个包含这些对象的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));

我使用了这个建议来创建序列化器。它运行良好,但我需要向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<...>。这不够优雅,但简单且有效。我不读取这些数据,只是将其写入并保存为日志文件。
或者我需要更改我的超级序列化程序吗?

更新。

我使用了下面的解决方案,但在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版本。

1个回答

3
与其尝试使用反射手动序列化你的MyObject类,不如使用XmlSerializer直接将你的字典值序列化为XElement,使用下面的方法,然后将结果包含在你正在构建的元素树中。
public static class XObjectExtensions
{
    public static XElement SerializeToXElement<T>(this IDictionary<string, T> collection, string collectionName)
    {
        return new XElement(collectionName, collection.Select(x => new XElement("Item", new XAttribute("Object", x.Key), x.Value.SerializeToXElement().Elements())));
    }

    public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = true)
    {
        var doc = new XDocument();
        using (var writer = doc.CreateWriter())
        {
            XmlSerializerNamespaces ns = null;
            if (omitStandardNamespaces)
                (ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
            (serializer ?? new XmlSerializer(obj.GetType())).Serialize(writer, obj, ns);
        }
        var element = doc.Root;
        if (element != null)
            element.Remove();
        return element;
    }
}

这将自动使 MyObject 的所有字段和属性正确序列化。使用它,生成的 XML 将如下所示:

<MyObjects>
  <Item Object="Obj1">
    <Str1>Test object</Str1>
    <E1>Unknown</E1>
    <Done>false</Done>
    <Codes>
      <Enum2>Code1</Enum2>
      <Enum2>Code2</Enum2>
    </Codes>
  </Item>
</MyObjects>

1
可以了!非常感谢!我不知道其中有什么魔法和区别,但它确实有效。 - Jury
我在Windows XP上遇到了异常。在其他系统上运行良好。我已经更新了我的问题。 - Jury
@Jury - 1)是否有内部异常或其他信息?2)尝试在该XP机器上安装任何补丁或更新。 XP的最新版本.Net是版本4,应该没问题。3)您能分享导致异常的类吗? - dbc
  1. 这是内部异常
  2. 我无法在这些机器上安装任何东西
  3. 不幸的是,我还没有访问此代码的权限。 我这样做:在XP上使用了我在问题中发布的序列化程序,而在其他操作系统上则使用了您的解决方案。XP的比例很小,所以这并不是很关键,但确实不太优雅。
- Jury

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接