c# - 如何在 linq to xml 中为此 xml 数据编写此查询?

标签 c# .net linq-to-xml

我一直在阅读 msdn 中的 linq to xml 文档和其他一些教程,但到目前为止我未能找到执行此查询的正确方法:(

基本上,我想遍历类的每个学生,对于每个问题,保留个人技能的计数器,即对于学生 123,计数应该是 001.101.033.002.001 - 1, 001.101.035.002.001 - 1, 001.101.033.002.002 - 0. 因为技能计数器是基于问题是正确的 (1) 还是错误的 (0)。

<assessment name="english101" level="primary 6">
   <class id="23" name="1A">
      <student id="123" name="Jack Black">
         <question id="101" correct="1">
            <skill id="001.101.033.002.001" topicId="033" subtopicId="002" subtopicdesc="Launching a browser" topicdesc="Point and Click">Able to recognize and use desktop icon</skill>

            <skill id="001.101.035.002.001" topicId="035" topicDesc="Typing" subtopicId="002" subtopicDesc="Using Words">Able to write on screen</skill>
         </question>

         <question id="102" correct="0">
            <skill id="001.101.033.002.002" topicId="033" subtopicId="002" subtopicdesc="Launching a browser" topicdesc="Point and Click">Able to recognize and use the mouse</skill>

            <skill id="001.101.035.002.001" topicId="035" topicDesc="Typing" subtopicId="002" subtopicDesc="Using Words">Able to write on screen</skill>
         </question>
      </student>

      <student id="124" name="Tim Robbins">
         <question id="103" correct="1">
            <skill id="001.101.033.002.002" topicId="033" subtopicId="002" subtopicdesc="Launching a browser" topicdesc="Point and Click">Able to recognize and use the mouse</skill>

            <skill id="001.101.035.002.001" topicId="035" topicDesc="Typing" subtopicId="002" subtopicDesc="Using Words">Able to write on screen</skill>
         </question>

         <dtasResult>
            <skill id="001.101.033.002.002" result="weak" />

            <skill id="001.101.033.002.002" result="strong" />
         </dtasResult>
      </student>
   </class>
</assessment>

到目前为止我的代码在这里:

    //Open up the xml and traverse to the student nodes.
                    XElement root = XElement.Load(fileLocation);
                    IEnumerable<XElement> students = root.Elements("class").Elements("student");

                    string currentStudentId = "";
                    foreach (XElement student in students)
                    {
                        currentStudentId = student.Attribute("id").ToString();
                        //this code chunk below doesn't work!
XElement questions = root.Descendants("question")
                                                .Where(question =>
                                                question.Attribute(""));

                        Console.WriteLine(student.DescendantsAndSelf("question"));
                        Console.WriteLine();
                        Console.WriteLine();
                    }

我还没有弄清楚如何为每个学生的每个问题做循环......并且 foraech 中间的代码不起作用!!

更新:

如果我想计算每个学生的总问题数,我将如何修改上面的查询来这样做?我尝试了 grp.Count(z => z.Correct) 和其他方法,但我做不对,谢谢 :)

最佳答案

二次编辑重新评论;我认为这可以满足您的需求...

var qry = from cls in doc.Root.Elements("class")
          from student in cls.Elements("student")
          from question in student.Elements("question")
          from skill in question.Elements("skill")
          select new {
            Student = (int)student.Attribute("id"),
            Skill = (string)skill.Attribute("id"),
            Correct = (int)question.Attribute("correct")
          } into denorm
          group denorm by new {
            denorm.Student,
            denorm.Skill
          } into grp
          orderby grp.Key.Student, grp.Key.Skill
          select new {
            grp.Key.Student,
            grp.Key.Skill,
            Tally = grp.Sum(x => x.Correct)
          };

foreach (var row in qry)
{
  Console.WriteLine("{0}\t{1}\t{2}",
    row.Student, row.Skill, row.Tally);
}

关于c# - 如何在 linq to xml 中为此 xml 数据编写此查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/801136/

相关文章:

android - 人脸识别dll

c# - 如何使用 C# 创建/导出到 .PVK 文件?

c# - ASP .NET Core 写入 XML 文件

c# - X文档异常: Root element is missing

c# - Unicode 字符串到 SQLite 数据库

c# - C# 中多重继承的替代解决方案的最佳实践是什么

c# - 无法添加 DbContext ASP.Net Core 或使用 Controller 访问数据

c# - 使用 Prism 进行跨模块通信?

c# - 解析 XDocument c# 中的命名空间

.net - 我怎么知道哪个运行时主机当前正在运行我的代码?