【问题标题】:C# Serialize nested lists as XMLC# 将嵌套列表序列化为 XML
【发布时间】:2016-01-14 11:46:48
【问题描述】:

我想将一个类序列化为具有 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();

}

以下是异常详情:

System.Xml.dll 中发生了“System.InvalidOperationException”类型的第一次机会异常 附加信息:无法生成临时类(结果=1)。 错误 CS0030:无法将类型“System.Collections.Generic.List”转换为“string[]” 错误 CS0029:无法将类型“string[]”隐式转换为“System.Collections.Generic.List”


有没有不创建自定义集合类的简单方法?

【问题讨论】:

  • I added the nested collection field, 我们应该猜测您的课程和代码(还是您要发布它们)?
  • 如果我切换到 List{List{String}},我会收到 CS0030、CS0029 和第二个 CS0030 错误。

标签: c# serialization collections xml-serialization


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多