如何使用 linq- c# 将 XML 元素值转换为 int

本文关键字:转换 int 元素 XML 何使用 linq- | 更新日期: 2023-09-27 18:31:16

我正在尝试从我的 XML 元素中获取值并使用 linq 命令将其转换为整数,以便我可以使用它做一些数学方程。

到目前为止,我就是这样尝试的:

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

但由于某种原因,该值仍为 0。

这是我的 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>

将非常感谢一些帮助。

如何使用 linq- c# 将 XML 元素值转换为 int

只需将XElement投射到 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());
    }        
}

请注意,您也可以强制转换为int?,当引用XElement时,其工作方式相同,但是如果您将空引用转换为int?则会返回一个空值 - 如果您不知道是否有相应的元素,这会很有帮助。

XElement有很多显式转换,XAttribute - 它们使生活变得更容易。

我很确定您的assessmentOneWeight集合不包含单个值,因为在 XML 中找不到comboBoxTab4Mod8.SelectedItem的值。

因此你总是得到0,因为a是用0初始化的。


顺便说一句:我强烈建议你改进你的代码。

循环访问 <Module> 元素而不是 <moduleTitle> 元素。这更有意义,因为您需要此元素的数据(即<assessmentOneWeight>

直接在 LINQ 语句中强制转换 <assessmentOneWeight> 的值,如下所示:

var assessmentOneWeight =
    from d in document.Descendants("moduleTitle")
    where d.Value == (String)comboBoxTab4Mod8.SelectedItem
    select int.Parse(d.Parent.Element("assessmentOneWeight").Value);

您的foreach设计不佳,因为它仅采用集合中找到的最后一个值。如果确实需要该值,请使用 LINQ 的强大功能并仅选择该值。