如何使用linq- C#中的XML元素值转换成int(How to convert an XML e

2019-09-16 13:50发布

我想获得的价值我的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>

将不胜感激一些帮助。

Answer 1:

刚投了XElementint

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? 你会得到一个空值回-它可以是有益的,如果你不知道是否相应的元素或没有。

有许多显式转换XElementXAttribute太-他们让生活变得更加简单。



Answer 2:

我敢肯定,你assessmentOneWeight集合不包含一个单一的价值,因为价值comboBoxTab4Mod8.SelectedItem不能在XML中找到。

因此你总是0 ,因为a与初始化0


BTW:我强烈建议你改善你的代码。

遍历<Module>元素,而不是<moduleTitle>元素。 这更有意义,因为你需要该元素的数据(即<assessmentOneWeight>

铸造的值<assessmentOneWeight >直接在您的LINQ语句,就像这样:

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

foreach没有很好地设计成只需要你的收藏中发现的最后一个值。 如果你真的想要的值使用LINQ的力量,只选择值。



文章来源: How to convert an XML elements value to an int using linq- c#