C#将嵌套列表序列化为XML

时间:2015-10-15 20:53:01

标签: c# serialization collections xml-serialization

我想将类序列化为具有List {List {String}}或String [] []或List {String []}类型字段的XML。在添加嵌套集合字段之前,我的类正在序列化和反序列化,但是在现在序列化或反序列化时它会抛出InvalidOperationException。

如果我必须为这个特定实例使用数组或列表,我真的不在乎,但是知道一个可用于任何嵌套集合情况的通用解决方案会很好。

目前我的字段声明如下:

[XmlElement("foo")]
public List<String[]> foo;

过去,我在单级列表和数组上运行良好。

以下是完整的课程:

[XmlRoot("ColumnUpdaterPrefs")]
public class ColumnUpdaterPrefs : Prefs {

    public ColumnUpdaterPrefs() : base() {
        defaultHeaders = new List<String[]>();
    }

    [XmlAttribute("autoFill")]
    public Boolean autoFill = true;

    [XmlAttribute("allowErrors")]
    public Boolean allowErrors;

    [XmlAttribute("allowZeroes")]
    public Boolean allowZeroes;

    [XmlElement("defaultHeaders")]
    public List<String[]> defaultHeaders;

    [XmlElement("defaultKey")]
    public String defaultKey;

    public override Object Clone() { 
        return new ColumnUpdaterPrefs() {
            autoFill = this.autoFill,
            allowErrors = this.allowErrors,
            allowZeroes = this.allowZeroes,
            defaultHeaders = this.defaultHeaders,
            defaultKey = this.defaultKey
        }; 
    }
}

及其基类:

[Serializable]
public abstract class Prefs : ICloneable {

    [XmlAttribute("name")]
    public String name;

    public Prefs(String name = null) {
        this.name = name;
    }

    public String Serialize() {
        var xs = new XmlSerializer(this.GetType()); //InvalidOperationException occurs here
        using (var sw = new StringWriter()) {
            xs.Serialize(sw, this);
            var result = sw.ToString();
            return result;
        }
    }

    public static TPrefs Deserialize<TPrefs>(String xml) 
        where TPrefs : Prefs {
        var xs = new XmlSerializer(typeof(TPrefs)); //InvalidOperationException occurs here
        using (var sr = new StringReader(xml)) {
            var result = (TPrefs)(xs.Deserialize(sr));
            return result;
        }
    }

    public void Write(ApplicationSettingsBase settings, Boolean save = false, String name = null) {
        if (settings == null) throw new ArgumentNullException("settings");

        if (name == null) name = this.name;
        settings[name] = Serialize();
        if (save) settings.Save();           
    }

    public static TPrefs Read<TPrefs>(ApplicationSettingsBase settings, String name)
        where TPrefs : Prefs {

        if (settings == null) throw new ArgumentNullException("settings");
        return Deserialize<TPrefs>((String)settings[name]);
    }

    public static TPrefs ReadOrDefault<TPrefs>(ApplicationSettingsBase settings, String name)
        where TPrefs : Prefs, new() {

        try { return Read<TPrefs>(settings, name); }
        catch { return new TPrefs() { name = name }; }
    }

    public abstract Object Clone();

}

以下是例外情况:

类型&#39; System.InvalidOperationException&#39;的第一次机会异常发生在System.Xml.dll中 附加信息:无法生成临时类(结果= 1)。 错误CS0030:无法转换类型&#39; System.Collections.Generic.List&#39;到&#39;字符串[]&#39; 错误CS0029:无法隐式转换类型&#39; string []&#39;到&#39; System.Collections.Generic.List&#39;

有没有一种简单的方法可以在不创建自定义集合类的情况下执行此操作?

1 个答案:

答案 0 :(得分:0)

感谢downvote,但事实证明问题只是使用了错误的属性;根据我的原始代码示例可以提出的建议。

[XmlArray("outerList")]
[XmlArrayItem("innerList", typeof(List<String>))]
public List<List<String>> foo;