我想阅读下面的 XML 并按以下顺序打印输出。你能帮忙查询吗?
10 学科 1 A1 10 学科 2 B1 10 学科 3 C1 20 学科 1 B2 20 学科 2 A1 20 学科 3 C2
提前致谢
这是一个示例,您可以如何以所需的形式进行查询:
XDocument doc = XDocument.Load("test.xml");
var query = from l in doc.Descendants("L1")
let id = l.Attribute("id").Value
from subject in l.Descendants("Subject")
select new
{
Id = id,
SubjectName = (string)subject.Attribute("SubjectName"),
Score = (string)subject.Attribute("Score")
};
foreach (var result in query)
{
Console.WriteLine(result);
}
输出:
{ Id = 10, SubjectName = Subject1, Score = A1 }
{ Id = 10, SubjectName = Subject2, Score = B1 }
{ Id = 10, SubjectName = Subject3, Score = C1 }
{ Id = 20, SubjectName = Subject1, Score = B2 }
{ Id = 20, SubjectName = Subject2, Score = A1 }
{ Id = 20, SubjectName = Subject3, Score = C2 }