我有一个列表框,其中包含来自和 xml 文件的标签名称,还有其他标签也包含在一个类中,但是当我尝试单击列表框中的值以将标签文本设置为里面的值时我收到以下错误:“InvalidCastException 未处理。无法将'System.String'类型的对象转换为'Coursework1.modules'类型。” 这是代码,这是在索引更改时的列表框事件内。
for (int i = 0; i < selectionBox.Items.Count; i++)
{
if (selectionBox.GetSelected(i) == true)
{
infoLabel.Text = ((modules)selectionBox.Items[i]).mInfo;
lectureSlotLabel.Text = ((modules)selectionBox.Items[i]).mLSlot;
tutorialLabel.Text = ((modules)selectionBox.Items[i]).mTSlot;
prerequisiteLabel.Text = ((modules)selectionBox.Items[i]).mPreReq;
codeLabel.Text = ((modules)selectionBox.Items[i]).mCode;
nameLabel.Text =((modules)selectionBox.Items[i]).mName;
}
}
//选择框创建
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + @"\XML.xml");
textReader.Read();
XmlNodeType type;
while (textReader.Read())
{
textReader.MoveToElement();
type = textReader.NodeType;
if (type == XmlNodeType.Element)
{
//if (textReader.Name == "Code")
//{
// module.Add(new modules(textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString(),
// textReader.ReadElementContentAsString()));
//}
if (textReader.Name == "Code")
{
textReader.Read();
code = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "Name")
{
textReader.Read();
name = textReader.Value;
//selectionBox.Items.Add(name);
Console.WriteLine(name);
}
if (textReader.Name == "Semester")
{
textReader.Read();
semester = textReader.Value;
Console.WriteLine(semester);
}
if (textReader.Name == "Prerequisite")
{
textReader.Read();
preReq = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "LectureSlot")
{
textReader.Read();
lSlot = textReader.Value;
Console.WriteLine(lSlot);
}
if (textReader.Name == "TutorialSlot")
{
textReader.Read();
tSlot = textReader.Value;
Console.WriteLine(tSlot);
}
if (textReader.Name == "Info")
{
textReader.Read();
info = textReader.Value;
Console.WriteLine(info);
module.Add(new Modules(code, name, semester, tSlot, lSlot, info, preReq));
}
}
//Console.WriteLine(module);
}
foreach (object o in module)
{
Modules m = (Modules)o;
//String hold = m.mName;
selectionBox.Items.Add(m.mName);
}
textReader.Close();
//正在使用的标签文本
var xmlFile = XDocument.Load(xmlDirectory + @"\XML.xml");
//Finds the XML file again and looks for the Name tag
var mName = from directory in xmlFile.Descendants("Name")
where directory.Value == (String)selectionBox.SelectedItem
select directory.Parent.Element("Name").Value;
//in the mName we search the directory for the Name tag and after we search
//for the selected item of the list in the directory after that we look for
//the "Name" element in the directory and that gets the name value and saves it to
//mName
foreach (var item in mName)
{
//use the loop to make it dynamic as when the selected item changes so does,
//the moduleName label
nameLabel.Text = item.ToString();
}