正在输出要列出的存储值类

本文关键字:存储 输出 | 更新日期: 2023-09-27 18:22:41

这将从XML文件加载一组值,并将它们放入一个类中进行存储。我正试图弄清楚如何将值输出为列表,以便将它们放入Listbox中。

我认为会有一种简单的方法,比如.ToList()方法,或者能够通过类中的字符串进行foreach(没有公共GetEnumerator)。我已经发现Foreach隐藏了一些复杂性,但并没有去做我想做的事。

我一直在网上搜索,但没有结果(可能缺乏正确的术语),不幸的是,我把C#参考书忘在了工作中:/

非常感谢指向正确方向的指针,谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace ThereIsOnlyRules
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            listBox1.Items.Clear();
            string path = "characterXML.xml";
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();
            CXML.Load(fs);
            //Get the number of elements 
            XmlNodeList elemList = CXML.GetElementsByTagName("unit");
            //foreach (var element in elemList)
            //{
            //    listBox1.Items.Add(element);
            //}
            for (int i = 0; i < elemList.Count; i++)
            {
                UnitAttributes attributes = new UnitAttributes();
                attributes.army = elemList[i].Attributes["army"].Value;
                attributes.category = elemList[i].Attributes["category"].Value;
                attributes.type = elemList[i].Attributes["type"].Value;
                attributes.composition = elemList[i].Attributes["composition"].Value;
                attributes.WS = elemList[i].Attributes["WS"].Value;
                attributes.BS = elemList[i].Attributes["BS"].Value;
                attributes.T = elemList[i].Attributes["T"].Value;
                attributes.W = elemList[i].Attributes["W"].Value;
                attributes.I = elemList[i].Attributes["I"].Value;
                attributes.A = elemList[i].Attributes["A"].Value;
                attributes.LD = elemList[i].Attributes["LD"].Value;
                attributes.save = elemList[i].Attributes["Save"].Value;
                attributes.armour = elemList[i].Attributes["armour"].Value;
                attributes.weapons = elemList[i].Attributes["weapons"].Value;
                attributes.specialrules = elemList[i].Attributes["specialrules"].Value;
                attributes.transport = elemList[i].Attributes["transport"].Value;
                attributes.options = elemList[i].Attributes["options"].Value;
                //foreach (string item in attributes)
                //{
                    //unit.Add(item);
                //}
                //listBox1.Items.AddRange(attributes)
            }  
            //Close the filestream
            fs.Close();
        }
        catch (Exception ex)
        {
        }
    }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ThereIsOnlyRules
{
class UnitAttributes
{
    public string army { get; set; }
    public string category { get; set; }
    public string type { get; set; }
    public string composition { get; set; }
    public string WS { get; set; }
    public string BS { get; set; }
    public string T { get; set; }
    public string W { get; set; }
    public string I { get; set; }
    public string A { get; set; }
    public string LD { get; set; }
    public string save { get; set; }
    public string armour { get; set; }
    public string weapons { get; set; }
    public string specialrules { get; set; }
    public string transport { get; set; }
    public string options { get; set; }
    }
}
<?xml version="1.0"?>
<config>
<unit
army="Tyranids"
category="Troops"
type="Infantry"
composition="10-30"
WS="3"
BS="3"
T="3"
W="1"
I="4"
A="1"
LD="6"
Save="6+"
armour="Chitin"
weapons="Claws and Teeth, Fleshborer"
specialrules="Instictive Behaviour - Lurk, Move Through Cover"
transport="If the brood consists of 20 models or less, it may take a Mycetic Spore."
options="Strangleweb, Spinefists, Spike rifle, Devourer, Adrenal Glands, Toxin Sacs"
>
Termagant Brood
</unit>
<unit
army="Tyranids"
category="Troops"
type="Infantry"
composition="10-30"
WS="3"
BS="3"
T="3"
W="1"
I="5"
A="2"
LD="6"
Save="6+"
armour="Chitin"
weapons="Scything Talons"
specialrules="Instictive Behaviour - Feed, Bounding Leap, Fleet, Move Through Cover"
transport="If the brood consists of 20 models or less, it may take a Mycetic Spore."
options="Adrenal Glands, Toxin Sacs"
>
Hormagaunt Brood
</unit>
</config>

正在输出要列出的存储值类

是类字段或属性的成员吗?无论哪种方式,在从XML文件中水合类的一个实例之后,一点反射和Linq应该允许您枚举类的所有数据成员。

var fieldDictionary = 
   (from f in typeof(UnitAttributes).GetFields()
    select new {Name = f.Name, Value = (string)(f.GetValue(attributes))})
   .ToDictionary(x=>x.Name, x=>x.Value);

fieldDictionary现在是Dictionary<string, string>(它是IEnumerable<KeyValuePair<string, string>>),应该适合加载到ListBox中。

被告知;反射是缓慢的。更可取的做法是修改或扩展UnitAttributes类以实现IEnumerable(可能是元组,也可能是KeyValuePair)。它还允许您按所需的顺序枚举类实例的属性,而不是按定义它们的顺序,或者按字段名称等其他FieldInfo/PropertyInfo数据枚举。

还要注意,字段不是属性,反之亦然。如果你的类中混合了属性和公共字段,我强烈建议将它们标准化;否则,您将不得不使用上面的两个Linq语句来反映属性列表和字段列表(成本大约是运行时间的两倍),并且它们不可能按照任何自定义的顺序。

如果使用常见的序列化程序(如XmlSerializer)来处理将对象转换为字符串或从字符串转换为对象,您将节省大量时间和精力。您不必从头开始编写这种类型的代码。