Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从分层类结构创建XML_C#_.net_Winforms_Visual Studio 2010_Xml Serialization - Fatal编程技术网

C# 从分层类结构创建XML

C# 从分层类结构创建XML,c#,.net,winforms,visual-studio-2010,xml-serialization,C#,.net,Winforms,Visual Studio 2010,Xml Serialization,我正在尝试根据类设置创建一个XML文件。我很难让它工作,目前我没有收到任何错误或异常,只是意外的结果 我期待着这样的事情 但我只是得到了 我想我可能对XML的继承权扩展得太远了,所以我试着一次一个注释出他们的最高级别的类,但仍然得到了相同的结果 如果您能指出正确的方向,我将不胜感激,谢谢 namespace ThereIsOnlyRules { [Serializable] public class ArmyListing { //[XmlElement("army")]

我正在尝试根据类设置创建一个XML文件。我很难让它工作,目前我没有收到任何错误或异常,只是意外的结果

我期待着这样的事情


但我只是得到了

我想我可能对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);
    }
}
namespace-ThereIsOnlyRules
{
[可序列化]
公营军营
{
//[XmlElement(“陆军”)]
//公共字符串名称{get;set;}
[XmlArray]
公共列表{get;set;}
public-void-xml(ArmyListing-ArmyListing)
{
尝试
{
XmlSerializer serializer=新的XmlSerializer(typeof(ArmyListing));
TextWriter TextWriter=newstreamWriter(@“C:\Test\40k.xml”);
序列化(textWriter,ArmyList);
textWriter.Close();
}
捕获(例外情况除外){}
}
}
[可序列化]
公营军
{
//公军();
//[XmlAttribute]
[XmlArray(“单位类别”)]
公共列表unitCategory{get;set;}
[XmlAttribute(“名称”)]
公共字符串armyName{get;set;}
}
[可序列化]
公共类单位类别
{
//公共单位类别();
[XmlArray(“单元类型”)]
公共列表单元类型{get;set;}
[XmlAttribute(“名称”)]
公共字符串unitCategoryName{get;set;}
}
[可序列化]
公共类单位类型
{
//公共单位类型();
[XmlArray(“单位”)]
公共列表单元{get;set;}
[XmlAttribute(“名称”)]
公共字符串unitTypeName{get;set;}
}
[可序列化]
公营课组
{
//公共单位();
[XmlAttribute(“名称”)]
公共字符串unitName{get;set;}
[XmlAttribute(“组合”)]
公共字符串组合{get;set;}
[XmlAttribute(“武器技能”)]
公共字符串武器技能{get;set;}
[XmlAttribute(“弹道技能”)]
公共弦弹道技能{get;set;}
[XmlAttribute(“强度”)]
公共字符串强度{get;set;}
[XmlAttribute(“韧性”)]
公共字符串T{get;set;}
[XmlAttribute(“伤口”)]
公共字符串{get;set;}
[XmlAttribute(“倡议”)]
公共字符串倡议{get;set;}
[XmlAttribute(“攻击”)]
公共字符串攻击{get;set;}
[XmlAttribute(“领导力”)]
公共字符串领导{get;set;}
[XmlAttribute(“保存抛出”)]
公共字符串saveThrow{get;set;}
[XmlAttribute(“装甲”)]
公共字符串{get;set;}
[XmlAttribute(“武器”)]
公共字符串{get;set;}
[XmlAttribute(“特殊规则”)]
公共字符串特殊规则{get;set;}
[XmlAttribute(“专用传输”)]
公共字符串专用传输{get;set;}
[XmlAttribute(“选项”)]
公共字符串选项{get;set;}
}
}
命名空间ThereIsOnlyRules
{
公共部分类Form1:Form
{
//ArmyList ArmyList=新的ArmyList();
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
ArmyList ArmyList=新的ArmyList();
陆军=新军();
UnitCategory UnitCategory=新的UnitCategory();
UnitType UnitType=新的UnitType();
单位列表=新单位();
armyListing.army=新列表();
army.unitCategory=新列表();
unitCategory.unitType=新列表();
unitType.unit=新列表();
army.armyName=“泰兰德”;
unitCategory.unitCategoryName=“部队”;
unitType.unitTypeName=“步兵”;
unitList.armor=“甲壳素”;
unitList.attacks=“3”;
单位列表。弹道技能=“100”;
unitList.composition=“20”;
unitList.indivatedTransport=“否”;
unitList.initiative=“3”;
unitList.leadership=“5”;
unitList.options=“8”;
unitList.saveThrow=“6+”;
unitList.specialRules=“无”;
unitList.strength=“3”;
unitList.T=“4”;
unitList.unitName=“Hornmagant”;
unitList.arms=“许多”;
unitList.weaponSkill=“3”;
unitList.1=“1”;
//列表单位=新列表();
//List unitType=new List();
//List unitCategory=新列表();
//列表=新列表();
//Dictionary armylist=新字典();
ArmyList.ml(ArmyList);
}
}

查看对象创建代码。您创建了所有这些对象,但从未将它们绑定在一起。结果是,您将一个完全空的ArmyList对象传递给序列化程序。这是正确的编码行为

armyListing.army.Add(陆军)


您将看到您开始获得一些输出。

这项任务非常有用的功能是,它从C#3.0开始引入

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


我看不出您在哪里将
army
添加到
armyLising.army
,或者在层次结构中设置任何其他子属性。有几个次要的风格点:类、方法和属性通常以大写字母开头,如果您使用属性,您可以使用对象初始值设定器来清理您的创建代码,如:
单位列表=新单位列表{armor=“Chitin”,攻击=3/…etc}
显然格式有点好,但你明白了。老实说,我这样做的唯一原因是,我可以从XML进行反向和反序列化,所以我不需要硬编码这些数据。不过,我以后会使用它!XML标记FormMatting让我困惑。不过这并不是毫无意义的,我会将属性连接到textboxes稍后。仅供参考
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);
}
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)
    {
    }
}