我有一个看起来像这样的xml文件;
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Family>
<ID>1</ID>
<Head_Model>FOV</Head_Model>
<Version>7</Version>
<Version>15</Version>
<Version>40</Version>
<Version>80</Version>
<Use>U</Use>
<Use>M</Use>
</Family>
<Family>
<ID>2</ID>
<Head_Model>CSK</Head_Model>
<Version>20</Version>
<Version>40</Version>
</Family>
<Family>
<ID>3</ID>
<Head_Model>EX</Head_Model>
</Family>
</Data>
在我的表格上我有3个组合框,用于型号,版本和用途。
我需要使用xml中的数据填充组合框,以便选择&#34; FOV&#34;那么只有它的版本应该在组合框2中显示,并在组合框3中使用。
我正在读取这样的xml,以便在加载时填充组合框一个;
public void LoadXML()
{
using (XmlReader reader = XmlReader.Create(XmlLocation))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
if (reader.Name == "ID")
{
reader.Read();
string sID = reader.Value;
string sHead_Model = string.Empty;
if (reader.ReadToFollowing("Head_Model"))
{
reader.Read();
sHead_Model = reader.Value;
}
if (!string.IsNullOrEmpty(sID) && sHead_Model != string.Empty)
lstItems.Add(new Item(sHead_Model, Convert.ToInt32(sID)));
}
}
}
comboBox1.DataSource = lstItems;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";
}
问题是能够过滤和填充框2我无法弄清楚如何使用值填充它,因为我只显示第一个<Value>
标记,并且它不随切换事件而改变。
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "FOV":
using (XmlReader reader = XmlReader.Create(XmlLocation))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
if (reader.Name == "ID")
{
reader.Read();
string sID = reader.Value;
string sHead_Model = string.Empty;
if (reader.ReadToFollowing("Head_Model"))
{
reader.Read();
sHead_Model = reader.Value;
string sVersion = string.Empty;
if (reader.ReadToFollowing("Version"))
{
reader.Read();
sVersion = reader.Value;
}
if (!string.IsNullOrEmpty(sID) && sHead_Model != string.Empty && sVersion != string.Empty && sHead_Model == "FOV")
VersionsItems.Add(new Item(sVersion, Convert.ToInt32(sID)));
}
}
}
}
comboBox2.DataSource = VersionsItems;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "Id";
break;
case "CSK":
using (XmlReader reader = XmlReader.Create(XmlLocation))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
if (reader.Name == "ID")
{
reader.Read();
string sID = reader.Value;
string sHead_Model = string.Empty;
if (reader.ReadToFollowing("Head_Model"))
{
reader.Read();
sHead_Model = reader.Value;
string sVersion = string.Empty;
if (reader.ReadToFollowing("Version"))
{
reader.Read();
sVersion = reader.Value;
}
if (!string.IsNullOrEmpty(sID) && sHead_Model != string.Empty && sVersion != string.Empty && sHead_Model == "CSK")
VersionsItems.Add(new Item(sVersion, Convert.ToInt32(sID)));
}
}
}
}
comboBox2.DataSource = VersionsItems;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "Id";
break;
}
}
答案 0 :(得分:1)
让我们为您的数据创建模型。
public class Data
{
[XmlElement("Family")]
public List<Family> Families { get; set; }
}
public class Family
{
[XmlElement("ID")]
public int Id { get; set; }
[XmlElement("Head_Model")]
public string HeadModel { get; set; }
[XmlElement("Version")]
public List<int> Version { get; set; }
[XmlElement("Use")]
public List<string> Use { get; set; }
}
反序列化xml并用数据填充模型
通过BindingSource
将组合框绑定到数据。
Data data;
var xs = new XmlSerializer(typeof(Data));
using (var stream = new FileStream("test.xml", FileMode.Open))
{
data = (Data)xs.Deserialize(stream);
}
var bs1 = new BindingSource();
bs1.DataSource = data;
bs1.DataMember = nameof(Data.Families);
comboBox1.DataSource = bs1;
comboBox1.DisplayMember = nameof(Family.HeadModel);
comboBox1.ValueMember = nameof(Family.Id);
var bs2 = new BindingSource();
bs2.DataSource = bs1;
bs2.DataMember = nameof(Family.Version);
comboBox2.DataSource = bs2;
var bs3 = new BindingSource();
bs3.DataSource = bs1;
bs3.DataMember = nameof(Family.Use);
comboBox3.DataSource = bs3;
当然,您可以为bs1
,bs2
,bs3
和data
创建类字段而不是局部变量。