且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#和Windows Phone 7解析XML

更新时间: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);
}