简体   繁体   English

序列化时出现InvalidOperationException

[英]InvalidOperationException when Serializing

I get an InvalidOperationException when trying to reflect the "listing" property, as said by inner exception. 在尝试反映“listing”属性时,我得到一个InvalidOperationException,如内部异常所述。 When it tries to serialize ArmyListing. 当它试图序列化ArmyListing时。

All the variables are public. 所有变量都是公开的。 Checked List can be serialized. Checked List可以序列化。 Most errors I found where with people using Dictionaries which can't. 我发现大多数错误都与人们使用字典无法做到。

Any idea why it appears not to be serializable? 知道为什么看起来不可序列化吗?

//[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:\Test\40k.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);
    }
}
}

This is the part that doesn't work: 这是不起作用的部分:

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

[XmlArray] & [XmlAttribute] can't both be defined on the same property. [XmlArray][XmlAttribute]不能同时在同一属性上定义。

If you keep drilling into .InnerException until you get to the original problem, the serializer even tells you this: 如果继续钻入.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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列化Protobuf时,XMLSerializer InvalidOperationException - XMLSerializer InvalidOperationException when serializing protobuf 使用IXmlSerializable对象序列化对象时出现InvalidOperationException - InvalidOperationException when Serializing an object with a IXmlSerializable object 序列化List的XmlSerializer的构造函数 <T> 与XmlAttributeOverrides一起使用时抛出InvalidOperationException - Constructor of a XmlSerializer serializing a List<T> throws an InvalidOperationException when used with XmlAttributeOverrides 使用protobuf-net序列化[Flags]枚举时出现InvalidOperationException - InvalidOperationException when serializing [Flags] enum with protobuf-net 序列化时出现Web Service和System.InvalidOperationException - Web Service and System.InvalidOperationException while serializing 使用线程时发生InvalidOperationException - InvalidOperationException when using threading 使WindowsFormsHost父级化时发生InvalidOperationException - InvalidOperationException when reparenting WindowsFormsHost 保存到DB时出现InvalidOperationException - InvalidOperationException when saving to DB 创建PerformanceCounter时发生InvalidOperationException - InvalidOperationException when creating PerformanceCounter 何时使用 InvalidOperationException 或 NotSupportedException? - When to use InvalidOperationException or NotSupportedException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM