从分层类结构创建XML

本文关键字:创建 XML 结构 分层 | 更新日期: 2023-09-27 18:23:51

我正在尝试基于类设置创建一个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);
    }
}

从分层类结构创建XML

查看您的对象创建代码。你把所有这些东西都做成新的,但你从来没有把它们绑在一起。最终发生的事情是将一个完全空的ArmyListing对象传递给序列化程序。这是正确的行为编码。

添加

armyListing.army.Add(army)

你会看到你开始得到一些输出。

对于这个任务非常有用的功能是Object和Collection Initializer,它是从C#3.0开始引入的。

以下是如何使用对象和集合初始值设定项(请注意,我对属性使用的是PascalCase,而不是camelCase):

public static void Test()
{
    UnitCategory troopsCategory = new UnitCategory
        {
            UnitCategoryName = "Troops",
            UnitType = new List<UnitType>
                {
                    new UnitType
                        {
                            UnitTypeName = "Infantry",
                            Unit = new List<Unit>
                                {
                                    new Unit
                                        {
                                            Armour = "Chitin",
                                            Attacks = "3",
                                            BallisticSkill = "100",
                                            Compsition = "20",
                                            DedicatedTransport = "No",
                                            Initiative = "3",
                                            Leadership = "5",
                                            Options = "8",
                                            SaveThrow = "6+",
                                            SpecialRules = "None",
                                            Strength = "3",
                                            T = "4",
                                            UnitName = "Hornmagant",
                                            Weapons = "Many",
                                            WeaponSkill = "3",
                                            Wounds = "1"                                               
                                        }
                                }
                        }
                }
        };
    Army army = new Army
    {
        ArmyName = "Tyranid",
        UnitCategory = new List<UnitCategory>
            {
                troopsCategory
            }
    };
    ArmyListing armyListing = new ArmyListing
    {
        Army = new List<Army>
                {
                    army
                }
    };
    armyListing.SerializeToXml(armyListing);
}

顺便说一下,using语句比手动关闭要好:

public void SerializeToXml(ArmyListing armyListing)
{
    try
    {
        var serializer = new XmlSerializer(typeof (ArmyListing));
        using (var textWriter = new StreamWriter(@"C:'Test'40k.xml"))
        {
            serializer.Serialize(textWriter, armyListing);
        }
    }
    catch (Exception ex)
    {
    }
}