更新时间:2022-03-03 09:24:59
Element
仅返回直接子节点.要递归浏览xml树,请改用Descendants
.
Element
only returns the immediate child node. To recursively browse the xml tree, use Descendants
instead.
要先列举学生然后再列举教师,您可以执行以下操作:
To successively enumerate the students then the teachers, you could do something like:
var xml = XDocument.Parse(myxmlstring, LoadOptions.None);
var students = xml.Descendants("students");
var teachers = xml.Descendants("teachers");
foreach (var studentElement in students.Descendants("person"))
{
MessageBox.Show(studentElement.Attribute("name").Value);
}
foreach (var teacherElement in teachers.Descendants("person"))
{
MessageBox.Show(teacherElement.Attribute("name").Value);
}