How to convert an XML elements value to an int using linq- c#

c#, linq-to-xml, type-conversion

Solution

Just cast the `XElement` to `int`:

private void buttonTab4Mod1Calculate_Click(object sender, EventArgs e)
{
    var document = XDocument.Load(workingDir + @"\Level4.xml");

    string selectedItem = (string) comboBoxTab4Mod8.SelectedItem;
    var assessmentOneWeight = from d in document.Descendants("moduleTitle")
                              where (string) d == selectedItem
                              select (int) d.Parent.Element("assessmentOneWeight");
    foreach (int item in assessmentOneWeight)
    {
         MessageBox.Show(item.ToString());
    }        
}

Note that you can also cast to `int?` which works the same way when the reference does is an `XElement`, but if you cast a null reference to `int?` you get a null value back - which can be helpful if you don't know whether there is a corresponding element or not.

There are lots of explicit conversions for `XElement`, and `XAttribute` too - they make life a lot easier.

Problem

I'm trying to get the value out of my XML element and convert it to an integer using linq commands so I can do some mathematical equations using it. This is how I've tried it so far: ``` private void buttonTab4Mod1Calculate_Click(object sender, EventArgs e) { var document = XDocument.Load(workingDir + @"\Level4.xml"); var assessmentOneWeight = from d in document.Descendants("moduleTitle") where d.Value == (String)comboBoxTab4Mod8.SelectedItem select d.Parent.Element("assessmentOneWeight").Value; int a = 0; foreach (var item in assessmentOneWeight) { a = Convert.ToInt32(item); } MessageBox.Show(a.ToString()); } ``` but the value is still 0 for some reason. This is my xml file: ``` <?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> ``` Would greatly appreciate some help.

Original source