2012-02-07 85 views
5

尝试反映“listing”属性(如内部异常所述)时,我得到InvalidOperationException。当它试图序列化军队列表。序列化时出现InvalidOperationException

所有变量都是公开的。 检查列表可以被序列化。我发现大部分错误都与使用字典的人不同。

任何想法为什么它看起来不是可序列化的?

//[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); 
    } 
} 
} 
+0

+1的Warhammer40k参考^ _ ^! – 2012-02-07 06:10:04

回答

10

这是不工作的一部分:

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

[XmlArray] & [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. 
+2

(添加了指向此的消息;希望没关系) – 2012-02-07 06:17:17

+0

Ty - 我应该想到这一点。 :-) – 2012-02-07 06:18:58

相关问题