我想用我的XML文件(我已经完成)中的值填充我的组合框,然后在标签中的值树下显示一些元素。一切都在标签页中
我在按下按钮时创建了这样的组合框:
int i = 0; //global variable for keeping track of combo box objects
int j = 0; // same but for labels
//the following codes are in the button click:
var document = XDocument.Load(workingDir + @"\Level4.xml");
comboBoxModuleSelect.Add(new ComboBox());
System.Drawing.Point p = new System.Drawing.Point(7, 57 + i * 25);
(comboBoxModuleSelect[i] as ComboBox).Location = p;
(comboBoxModuleSelect[i] as ComboBox).Size = new System.Drawing.Size(183, 20);
tabPageLevel4.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
并像这样填充它们:
var moduleName = from d in document.Descendants("moduleTitle")
select d.Value;
foreach (var item in moduleName)
{
//add the module names into the dropdown for students to select
(comboBoxModuleSelect[i] as ComboBox).Items.Add(item);
}
现在我想在用户选择的当前组合框中获取所选值,并检索与该值相关的一些XML元素。但是我在这一行得到了NullReferenceException:
var assessment = from d in document.Descendants("moduleTitle")
where d.Value == (comboBoxModuleSelect[i] as ComboBox).SelectedItem.ToString()
select d.Parent.Element("assessmentType").Value;
on comboBoxModuleSelect[i]
如果需要,XML如下:
<?xml version="1.0" encoding="utf-8" ?>
<Course>
<CourseName>BEng Software Engineering</CourseName>
<Modules>
<Module>
<moduleCode>ECSE401</moduleCode>
<moduleTitle>Programming Methodology</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Test2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>ECSC404</moduleCode>
<moduleTitle>Computer Systems Fundamentals</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Test1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Test2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Test3</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>EBSY401</moduleCode>
<moduleTitle>Information and Data Modelling</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Test</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>25</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>10</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>35</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework3</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>ECSC405</moduleCode>
<moduleTitle>Software Development Principles</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Test1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Test2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>ECSC407</moduleCode>
<moduleTitle>Web Technology</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Tutorials</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>20</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>20</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Exam</assessmentName>
<assessmentType>Exam</assessmentType>
<assessmentWeighting>60</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>ECSC409</moduleCode>
<moduleTitle>Software Engineering Principles</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Test1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Coursework2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>ECSC408</moduleCode>
<moduleTitle>Mathematics for Computing</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>50</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Exam</assessmentName>
<assessmentType>Exam</assessmentType>
<assessmentWeighting>50</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
<Module>
<moduleCode>EBSY400</moduleCode>
<moduleTitle>Communication and Learning Skills</moduleTitle>
<credits>15</credits>
<assessmentDetails>
<assessment>
<assessmentName>Presentation</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
</assessment>
<assessment>
<assessmentName>Portfolio</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>70</assessmentWeighting>
</assessment>
</assessmentDetails>
</Module>
</Modules>
</Course>
为什么我会收到nullreferenceexception?
答案 0 :(得分:1)
尝试将.SelectedItem.ToString()更改为。文本 as comboBox_type_boat.Text返回选定的值 SelectedItem.ToString()剂量返回从XML填充的组合框的值