序列化时出现InvalidOperationException



正如内部异常所说,当我试图反映"listing"属性时,我得到了一个InvalidOperationException。当它试图序列化ArmyListing时。

所有变量都是公共的。"选中的列表"可以序列化。我发现的大多数错误都是在人们使用字典时发现的,而字典却不能。

知道为什么它看起来不可序列化吗?

//[Serializable]
public class ArmyListing
{
    [XmlElement("army")]
    public List<Army> listing { get; set; }
    public void SerializeToXML(ArmyListing armyListing)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ArmyListing));
            TextWriter textWriter = new StreamWriter(@"C:Test40k.xml");
            serializer.Serialize(textWriter, armyListing);
            textWriter.Close();
        }
        catch (Exception ex) { }
    }
}
//[Serializable]
public class Army
{
    //public Army();
    [XmlAttribute]
    [XmlArray("unit-category")]
    public List<UnitCategory> settingconstraints { get; set; }
    [XmlAttribute("name")]
    public string armyName { get; set; }
}
//[Serializable]
public class UnitCategory
{
    //public UnitCategory();
    [XmlArray("unit-type")]
    public List<UnitType> setting { get; set; }
    [XmlAttribute("name")]
    public string unitCategoryName { get; set; }
}
//[Serializable]
public class UnitType
{
    //public UnitType();
    [XmlArray("unit")]
    public List<Unit> setting { get; set; }
    [XmlAttribute("name")]
    public string unitTypeName { get; set; }
}
//[Serializable]
public class Unit
{
    //public Unit();
    [XmlAttribute("name")]
    public string unitName { get; set; }
    [XmlAttribute("composition")]
    public string compsition { get; set; }
    [XmlAttribute("weapon-skill")]
    public string weaponSkill { get; set; }
    [XmlAttribute("ballistic-skill")]
    public string ballisticSkill { get; set; }
    [XmlAttribute("strength")]
    public string strength { get; set; }
    [XmlAttribute("toughness")]
    public string T { get; set; }
    [XmlAttribute("wounds")]
    public string wounds { get; set; }
    [XmlAttribute("initiative")]
    public string initiative { get; set; }
    [XmlAttribute("attacks")]
    public string attacks { get; set; }
    [XmlAttribute("leadership")]
    public string leadership { get; set; }
    [XmlAttribute("saving-throw")]
    public string saveThrow { get; set; }
    [XmlAttribute("armour")]
    public string armour { get; set; }
    [XmlAttribute("weapons")]
    public string weapons { get; set; }
    [XmlAttribute("special-rules")]
    public string specialRules { get; set; }
    [XmlAttribute("dedicated-transport")]
    public string dedicatedTransport { get; set; }
    [XmlAttribute("options")]
    public string options { get; set; }
}
 //Form
namespace ThereIsOnlyRules
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        ArmyListing armyListing = new ArmyListing();
        armyListing.SerializeToXML(armyListing);
    }
}
}

这是不起作用的部分:

[XmlAttribute]
[XmlArray("unit-category")]

CCD_ 1&不能在同一属性上同时定义[XmlAttribute]

如果您一直深入.InnerException,直到找到原始问题,序列化程序甚至会告诉您:

There was an error reflecting type 'ArmyListing'.
There was an error reflecting property 'listing'.
There was an error reflecting type 'Army'.
There was an error reflecting property 'settingconstraints'.
XmlAttribute and XmlAnyAttribute cannot be used in conjunction with XmlElement, XmlText, XmlAnyElement, XmlArray, or XmlArrayItem.

相关内容

  • 没有找到相关文章

最新更新