【发布时间】:2012-02-09 17:30:57
【问题描述】:
我正在尝试根据类设置创建一个 XML 文件。我无法让它工作,目前我没有收到任何错误或异常,只是意外结果。
我期待这样的事情
<armyListing><army name="">
<unit-category name="">
<unit-type name="">
<unit name="" composition="" weapon-skill="" etc></unit>
</unit-type>
<unit-type name="">
<unit name="" composition="" weapon-skill="" etc></unit>
</unit-type>
</unit-category>
</army>
</armyListing>
但我只收到<armyListing><army/></armyListing>
我认为我可能将 XML 的层次结构扩展得太远了,所以我尝试一次注释掉他们的最高类,但仍然得到相同的结果。
希望您能指出正确的方向,谢谢!
namespace ThereIsOnlyRules
{
[Serializable]
public class ArmyListing
{
//[XmlElement("army")]
//public string name { get; set; }
[XmlArray]
public List<Army> army { 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> unitCategory { get; set; }
[XmlAttribute("name")]
public string armyName { get; set; }
}
[Serializable]
public class UnitCategory
{
//public UnitCategory();
[XmlArray("unit-type")]
public List<UnitType> unitType { get; set; }
[XmlAttribute("name")]
public string unitCategoryName { get; set; }
}
[Serializable]
public class UnitType
{
//public UnitType();
[XmlArray("unit")]
public List<Unit> unit { 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; }
}
}
namespace ThereIsOnlyRules
{
public partial class Form1 : Form
{
//ArmyListing armyListing = new ArmyListing();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ArmyListing armyListing = new ArmyListing();
Army army = new Army();
UnitCategory unitCategory = new UnitCategory();
UnitType unitType = new UnitType();
Unit unitList = new Unit();
armyListing.army = new List<Army>();
army.unitCategory = new List<UnitCategory>();
unitCategory.unitType = new List<UnitType>();
unitType.unit = new List<Unit>();
army.armyName = "Tyranid";
unitCategory.unitCategoryName = "Troops";
unitType.unitTypeName = "Infantry";
unitList.armour = "Chitin";
unitList.attacks = "3";
unitList.ballisticSkill="100";
unitList.compsition="20";
unitList.dedicatedTransport = "No";
unitList.initiative = "3";
unitList.leadership = "5";
unitList.options = "8";
unitList.saveThrow = "6+";
unitList.specialRules ="None";
unitList.strength = "3";
unitList.T = "4";
unitList.unitName = "Hornmagant";
unitList.weapons = "Many";
unitList.weaponSkill = "3";
unitList.wounds = "1";
//List<Unit> unit = new List<Unit>();
//List<UnitType> unitType = new List<UnitType>();
//List<UnitCategory> unitCategory = new List<UnitCategory>();
//List<Army> army = new List<Army>();
//Dictionary<ArmyListing, Army> armylist = new Dictionary<ArmyListing,Army>();
armyListing.SerializeToXML(armyListing);
}
}
【问题讨论】:
-
我看不到您将
army添加到armyLising.army的位置,或者在您的层次结构中设置任何其他子属性。 -
几个小风格点:类、方法和属性通常以大写字母开头,如果您使用属性,则可以使用对象初始化器来清理您的创建代码,如下所示:
UnitList list = new UnitList { Armour = "Chitin", Attacks = 3 //...etc };显然格式更好一些,但你明白了。 -
老实说,我这样做的唯一原因是我可以反向执行它并从 XML 反序列化,所以我不需要对这些数据进行硬编码。不过,我将来会使用它! XML 标记格式让我感到困惑。不过,这并非毫无意义,稍后我会将属性连接到文本框。
-
仅供参考,
[Serializable]不被 XML 序列化程序使用。另外,当你吃所有异常时,你怎么知道没有异常。不要那样做。 -
@JohnSaunders:你能解释一下“吃掉所有异常”是什么意思吗......只要我有可以在 Try-Catch 中抛出异常的代码部分,就不应该有任何未处理的?我不清楚最佳实践或其他我已经打破的东西。
标签: c# .net winforms visual-studio-2010 xml-serialization