我想用我的 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;
上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?