كيفية كتابة هذا الاستعلام عن هذه البيانات أكس في LINQ إلى XML؟

StackOverflow https://stackoverflow.com/questions/801136

  •  03-07-2019
  •  | 
  •  

سؤال

ولقد تم القراءة من خلال LINQ إلى وثائق XML في MSDN وبعض الدروس الأخرى، وحتى الآن أنا فشلت في العثور على الطريق الصحيح لتحقيق هذا الاستعلام حتى الآن: (

وأساسا، أريد أن يتكرر خلال كل طالب في الصف، عن كل سؤال، والحفاظ على عداد من المهارة الفردية، أي للطالب 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.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);
}

نصائح أخرى

ولست متأكدا بالضبط ما إذا كان هذا هو ما تريد، ولكنني سوف إعطائها:

foreach (XElement student in students)
        {
            currentStudentId = student.Attribute("id").ToString();
            Console.WriteLine(currentStudentId);
            //this code chunk below doesn't work!
            IEnumerable<XElement> correctQuestions = student.Descendants("question").
                                  Where(q => q.Attribute("correct").Value == "1");                
            //Here you would do same for false answers

            //loop through correct answers
            foreach (XElement q in correctQuestions)
            {
                IEnumerable<XElement> skills = q.Descendants("skill");
                foreach (XElement s in skills)
                {
                    //this prints "001.101.035.002.001" etc  
                    Console.WriteLine(s.Attribute("id").Value);
                }
            }
            Console.WriteLine();
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top