a7omiton
a7omiton

Reputation: 1617

Comparing values in XML using C#

I'm trying to make my code say "if there is no component 4 in the module, display nothing". I'm reading the values from an XML file which gives details of a module (name,credits,assessments,etc) and am writing those values out using LINQ query commands.

Everything is working fine, but I ran into a problem regarding a requirement I want to set. Basically, every module has different number of assessments, some have 2,3 or 4. When I need to display details of a module that has a total of 4 assessments the code runs fine, however when there are either 2 or 3 assessments I get a nullreferenceexception at this line:

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

I know why its thrown, because there is no assessmentFour value for the module I've selected in my comboBox, and so I tried to solve it using the following:

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

but it doesn't work. I don't know how else to avoid the null reference exception, if someone could point it out for me I would appreciate it.

Here is the XML file if needed to understand:

<?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>

notice how each module doesn't have the same number of assessments, which is why I'm getting the error. I know I could probably add an assessmentFour element to each module and do it that way, but that's really cheap. If someone could help me please I could appreciate it. Thank you

Upvotes: 0

Views: 306

Answers (2)

Steve
Steve

Reputation: 543

Here's another approach that seemed to work for me:

        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);

Upvotes: 0

Jason
Jason

Reputation: 89127

You could try this...

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

then define this function further down in your class

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

  return x.Value;
}

Upvotes: 1

Related Questions