如何使用 XMLSerializer 序列化带有对象的非泛型 IList [英] How to Seralize non-generic IList with Objects with XMLSerializer

查看:23
本文介绍了如何使用 XMLSerializer 序列化带有对象的非泛型 IList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化一些包含带有子元素的 IList 的对象.IList 是非通用的,我不能让它成为通用的.我得到了像The Type MyObjectChild is not expected. ..."之类的异常,它说明了一些关于 XMLInclude 的内容,但我认为这不是我要搜索的内容.有谁知道如何让 XMLSerialzer 也序列化子元素?两个元素都是 [Serializable]

I'm trying to Serialize some Object, that contain an IList with Children Elements. The IListis non-generic and i cannot just make it at generic one. I get the exception like "The Type MyObjectChild was not expected. ..." and it says something about XMLInclude, but i don't think thats what I'm searching. Does anyone have any idea how bring XMLSerialzer to serialize the child elements as well? Both Elements are [Serializable]

XmlSerializer xmlSerializer = new XmlSerializer(typeof ObservableCollection<MyEntryViewItem>));
FileStream fileStream = new FileStream(exportPath, FileMode.Create);

xmlSerializer.Serialize(fileStream, _listEntries);

public IList ElementChildren { get { return _item.Children; } set { _item.Children = value; RaisePropertyChanged(() => ElementChildren); } }

[Serializable]
public sealed class ElementChild
{
    private int _id;
    private string _text;
    private string _path;
    ...
}

推荐答案

根据 Simon Hewitt 您可以使用 XmlInclude 来指定列表的类型.可以在 roboxman.wordpress.com

According to Simon Hewitt you can just use the XmlInclude to specify the type of your list. An alternative can be found at roboxman.wordpress.com

这篇关于如何使用 XMLSerializer 序列化带有对象的非泛型 IList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆