使用C#比较XML中的值

时间:2012-04-15 13:42:04

标签: c# linq-to-xml nullreferenceexception

我正在尝试让我的代码说“如果模块中没有组件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

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

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元素并以这种方式执行,但这非常便宜。如果有人可以帮助我,我会很感激。谢谢

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);