0

我试图让我的代码说“如果模块中没有组件 4,则不显示任何内容”。我正在从提供模块详细信息(名称、信用、评估等)的 XML 文件中读取值,并使用 LINQ 查询命令将这些值写出。

一切正常,但我遇到了关于我要设置的要求的问题。基本上,每个模块都有不同数量的评估,有些有 2,3 或 4。当我需要显示总共有 4 个评估的模块的详细信息时,代码运行良好,但是当有 2 或 3 个评估时,我得到此行的 nullreferenceexception:

var assessmentFour = from d in document.Descendants("moduleTitle")
                                 where d.Value == (String)comboBoxTab4Mod1.SelectedItem
                                 select d.Parent.Element("assessmentFour").Value; //exception thrown here

我知道它为什么被抛出,因为我在组合框中选择的模块没有assessmentFour 值,所以我尝试使用以下方法解决它:

foreach (var item in assessmentFour)
        {
            if (assessmentFour != null)
            {
                labelTab4Mod1Cmpt4.Text = "";
            }
            else
            {
                labelTab4Mod1Cmpt4.Text = item.ToString();
            }
        }

但它不起作用。我不知道如何避免空引用异常,如果有人可以为我指出,我将不胜感激。

如果需要理解,这里是 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<SoftwareEngineering>
  <Module>
    <moduleCode>ECSE401</moduleCode>
    <moduleTitle>Programming Methodology</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Coursework</assessmentOne>
    <assessmentOneWeight>40</assessmentOneWeight>
    <assessmentTwo>Coursework</assessmentTwo>
    <assessmentTwoWeight>40</assessmentTwoWeight>
    <assessmentThree>Test</assessmentThree>
    <assessmentThreeWeight>20</assessmentThreeWeight>
  </Module>
  <Module>
    <moduleCode>ECSC404</moduleCode>
    <moduleTitle>Computer Systems Fundamentals</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Test1</assessmentOne>
    <assessmentOneWeight>30</assessmentOneWeight>
    <assessmentTwo>Test2</assessmentTwo>
    <assessmentTwoWeight>30</assessmentTwoWeight>
    <assessmentThree>Test3</assessmentThree>
    <assessmentThreeWeight>40</assessmentThreeWeight>
  </Module>
  <Module>
    <moduleCode>EBSY401</moduleCode>
    <moduleTitle>Information and Data Modelling</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Test</assessmentOne>
    <assessmentOneWeight>25</assessmentOneWeight>
    <assessmentTwo>Coursework1</assessmentTwo>
    <assessmentTwoWeight>10</assessmentTwoWeight>
    <assessmentThree>Coursework2</assessmentThree>
    <assessmentThreeWeight>35</assessmentThreeWeight>
    <assessmentFour>Coursework3</assessmentFour>
    <assessmentFourWeight>30</assessmentFourWeight> 
  </Module>
  <Module>
    <moduleCode>ECSC405</moduleCode>
    <moduleTitle>Software Development Principles</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Test1</assessmentOne>
    <assessmentOneWeight>30</assessmentOneWeight>
    <assessmentTwo>Coursework</assessmentTwo>
    <assessmentTwoWeight>40</assessmentTwoWeight>
    <assessmentThree>Test2</assessmentThree>
    <assessmentThreeWeight>30</assessmentThreeWeight>
  </Module>
  <Module>
    <moduleCode>ECSC407</moduleCode>
    <moduleTitle>Web Technology</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Tutorials</assessmentOne>
    <assessmentOneWeight>20</assessmentOneWeight>
    <assessmentTwo>Coursework</assessmentTwo>
    <assessmentTwoWeight>20</assessmentTwoWeight>
    <assessmentThree>Exam</assessmentThree>
    <assessmentThreeWeight>60</assessmentThreeWeight>
  </Module>
  <Module>
    <moduleCode>ECSC409</moduleCode>
    <moduleTitle>Software Engineering Principles</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Coursework1</assessmentOne>
    <assessmentOneWeight>40</assessmentOneWeight>
    <assessmentTwo>Coursework2</assessmentTwo>
    <assessmentTwoWeight>30</assessmentTwoWeight>
    <assessmentThree>Coursework3</assessmentThree>
    <assessmentThreeWeight>30</assessmentThreeWeight>
  </Module>
  <Module>
    <moduleCode>ECSC408</moduleCode>
    <moduleTitle>Mathematics for Computing</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Coursework</assessmentOne>
    <assessmentOneWeight>50</assessmentOneWeight>
    <assessmentTwo>Exam</assessmentTwo>
    <assessmentTwoWeight>50</assessmentTwoWeight>
  </Module>
  <Module>
    <moduleCode>EBSY400</moduleCode>
    <moduleTitle>Communication and Learning Skills</moduleTitle>
    <credits>15</credits>
    <assessmentOne>Coursework</assessmentOne>
    <assessmentOneWeight>30</assessmentOneWeight>
    <assessmentTwo>Coursework</assessmentTwo>
    <assessmentTwoWeight>70</assessmentTwoWeight>
  </Module>
</SoftwareEngineering>

注意每个模块没有相同数量的评估,这就是我收到错误的原因。我知道我可以为每个模块添加一个assessmentFour 元素并这样做,但这真的很便宜。如果有人可以帮助我,我会很感激。谢谢

4

2 回答 2

1

你可以试试这个...

from d in document.Descendants("moduleTitle")
where d.Value == (String)comboBoxTab4Mod1.SelectedItem
select CheckNullElement(d.Parent.Element("assessmentFour"))

然后在你的类中进一步定义这个函数

private string CheckNullElement(XElement x)
{
  if (x == null) return string.Empty;

  return x.Value;
}
于 2012-04-15T13:55:27.140 回答
0

这是另一种似乎对我有用的方法:

        var assessmentFour = from d in doc.Descendants("Module")
                             where (string)d.Element("moduleTitle") == (String)comboBoxTab4Mod1.SelectedItem
                                         let el = d.Elements("assessmentFour").FirstOrDefault()
                                         select (el == null ? String.Empty : (string)el);
于 2012-04-15T14:55:46.237 回答