c#组合框中的变量列表(XML)
本文关键字:列表 XML 变量 组合 | 更新日期: 2023-09-27 18:18:28
我不知道如何将变量放入组合框中。我做了一些研究,似乎存储这些元素的列表是一个完美的元素容器,但让列表工作是另一回事。
无论如何,这是我使用的代码:
private List<string> HNames = new List<string>();
// ^ This is stored above `InitializeComponent();`
void LoadXML()
{
string path = "Mods//handling4.meta";
var doc = XDocument.Load(path);
var items = doc.Descendants("HandlingData").Elements("Item");
var query = from x in items
select new
{
HandlingName = (string)x.Element("handlingName"),
HandlingType = (string)x.Element("HandlingType"),
Mass = (decimal?)x.Element("fMass").Attribute("value"),
InitialDragCoeff = (decimal?)x.Element("fInitialDragCoeff").Attribute("value"),
PercentSubmerged = (decimal?)x.Element("fPercentSubmerged").Attribute("value"),
DriveBiasFront = (decimal?)x.Element("fDriveBiasFront").Attribute("value"),
InitialDriveGears = x.Element("nInitialDriveGears").Attribute("value"),
InitialDriveForce = (decimal?)x.Element("fInitialDriveForce").Attribute("value"),
DriveInertia = (decimal?)x.Element("fDriveInertia").Attribute("value")
};
foreach(var item in query)
{
HNames.Add(item.HandlingName);
}
}
private void button2_Click(object sender, EventArgs e)
{
comboBox1.Items.Add( HNames);
}
问题是,现在我在点击按钮后在我的组合框中看到'Collection'这个词,而不是实际的处理名称。
所以我的问题是:如何在comboBox内使用变量列表?
进一步信息:我是编程新手!这是一个windows表单API,用c#编写的
任何帮助或指导将不胜感激,如果你能评论一些关键词,我应该谷歌这是有帮助的,如果你理解这个问题&可以得到一个有用的答案。我已经研究了几个小时了,一定是遗漏了什么
看一下XmlSerializer。您可以直接将XML读入c# List对象,而不是像当前那样遍历元素。这将更容易从。
加载。例如:using (FileStream stream = new FileStream(myXmlFileName, FileMode.Open, FileAccess.Read))
{
using (XmlTextReader textReader = new XmlTextReader(stream))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyItemList));
comboBox1.DataSource = xmlSerializer.Deserialize(textReader);
}
}
您正在将集合添加到组合框中。您需要将每个集合项添加到组合框中。
:
foreach(var item in collection)
Combobox.Items.add (item)
YourCombobox。Datasource = your list
YourComboxbox。displaymember = "yourPropertyName"
要获得对象之后,你可以使用yourcomboxbox . selecteitem . databounditem和cast Tvis Object:)