LINQ에서 XML 에서이 XML 데이터에 대한이 쿼리를 작성하는 방법은 무엇입니까?
-
03-07-2019 - |
문제
MSDN과 다른 튜토리얼의 LINQ에서 XML 문서를 읽고 있었지만 지금은이 쿼리를 수행하는 올바른 방법을 찾지 못했습니다.
기본적으로, 나는 수업 시간에 각 학생을 통해 각 질문에 대해 개별 기술에 대한 반대, 즉 학생의 경우 123, 탈리는 001.101.033.002.001-1, 001.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();
}
나는 아직 학생당 각 질문에 대한 루핑을 수행하는 방법을 아직 찾지 못했습니다.
업데이트 :
학생당 총 질문을 계산하려면 위의 쿼리를 어떻게 수정할 수 있습니까? 나는 grp.count (z => z.correct) 및 기타 방법을 시도하지만 올바르게 얻을 수는 없습니다. :)
해결책
두 번째 편집 re 댓글; 나는 이것이 당신이 원하는 일을한다고 생각합니다 ...
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();
}
제휴하지 않습니다 StackOverflow