0

我有一个具有下一个结构的 xml:

<main>
   <students>
      <student>
           <name> John </name>
           <phone> 123 </phone>
           <courses>
                  <course>
                       <mark> 5 </mark>
                        <room> 11D </room> 
                        <name> Math </name>
                  </course>
                  <course>
                       <mark> 8 </mark>
                        <room> 12a </room> 
                        <name> Literature </name>
                  </course>
           </courses>
      </student>

      <student>
           <name> Terry </name>
           <phone> 332 </phone>
           <courses>
                  <course>
                       <mark> 9 </mark>
                        <room> 12D </room> 
                        <name> Math </name>
                  </course>
                  <course>
                       <mark> 4 </mark>
                        <room> 2a </room> 
                        <name> Literature </name>
                  </course>
           </courses>
      </student>
   </students>

   <profs>
   ....
   </profs>
</main>

如何获取这种格式的信息:

约翰->数学,5;文学,8 特里 -> 数学,9;文学,4

我想要做的是创建一个这样的树列表:

约翰 | _数学 - 5 |_文学 - 8 特里 | _数学 - 9 |_文学 - 4

我管理它以提取学生的姓名并将它们作为节点放在树列表中。

4

3 回答 3

2

看看 XElement 类看看这个例子

您可以遍历节点,甚至可以为此定义 linq 查询:

        XElement allData = XElement.Load("Authors.xml"); 
        if (allData != null) 
        { 
            IEnumerable<XElement> authors = allData.Descendants("Author"); 
            foreach(XElement author in authors) 
                Console.WriteLine((string)author); 
        }

看看这个例子

    string t = "Some title";
    var v = from page in _x.Elements("SitePage")
        where t == page.Element("Title").Value
        select page;
于 2013-02-18T12:54:52.187 回答
1

Okay so what you want is essentially a dictionary holding a string and another dictionary holding a string and a list.

var xml = XElement.Parse("");
var students = xml.Descendants("student");

students.ToDictionary(x => x.Element("name").Value,
    x => x.Descendants("course")
        .ToDictionary(y => y.Element("name").Value,
            y => int.Parse(y.Element("mark").Value)));

This creates a Dictionary<string, Dictionary<string, int>> where the first key is your student's name, and the second is a dictionary of course names and marks.

于 2013-02-18T13:05:03.357 回答
0
var allData = XElement.Parse("");
var query = from student in allData.Descendants("student")
            let name = student.Element("name").Value
            select new {
                        StudentName = name , 
                        Courses = 
                                 (from course in student.Descendants("course")
                                  let mark = course.Element("mark").Value
                                  let courseName = course.Element("name").Value
                                  select new { 
                                          CourseName = courseName , 
                                          Mark = mark }
                                  )
                         };    

您可能需要检查空值。我知道这不是很容易调试。

[编辑] allData.Descendants("student")不为"student"大写。

测试 :

foreach (var student in query)
{
    Console.WriteLine(student.StudentName);
    foreach (var course in student.Courses)
    {
        Console.WriteLine(string.Format(" - {0} => {1}", course.CourseName, course.Mark));
    }
}
于 2013-02-18T13:09:06.617 回答