循环遍历XmlDocument的XML子节点

时间:2016-10-21 23:30:48

标签: c# xml wpf

我正在用C#编写一个需要显示XML文档数据的WPF应用程序。这是XML文档:

<Schedules>
  <Schedule>
    <Course>
      <ID>CIT160</ID>
      <Section>01</Section>
      <Name>Introduction to Programming</Name>
      <Days>TuTh</Days>
      <STime>09:30</STime>
      <ETime>11:00</ETime>
      <Location>ATHS/E246</Location>
      <Teacher>KREPSHAW, R</Teacher>
    </Course>
    <Course>
      <ID>CIT180</ID>
      <Section>01</Section>
      <Name>Introduction to Database</Name>
      <Days>MW</Days>
      <STime>12:30</STime>
      <ETime>14:00</ETime>
      <Location>ATHS/E235</Location>
      <Teacher>SINGH, M</Teacher>
    </Course>
  </Schedule>
  <Schedule>
    <Course>
      <ID>CIT160</ID>
      <Section>01</Section>
      <Name>Introduction to Programming</Name>
      <Days>TuTh</Days>
      <STime>09:30</STime>
      <ETime>11:00</ETime>
      <Location>ATHS/E246</Location>
      <Teacher>KREPSHAW, R</Teacher>
    </Course>
    <Course>
      <ID>CIT180</ID>
      <Section>25</Section>
      <Name>Introduction to Database</Name>
      <Days>MW</Days>
      <STime>17:00</STime>
      <ETime>18:30</ETime>
      <Location>ATHS/E235</Location>
      <Teacher>SINGH, M</Teacher>
    </Course>
  </Schedule>
</Schedules>

我需要输出以下内容:

  

附表1

     

CIT160 01 Introdc ... TuTh 09:30 11:00 ATHS / E246 KREPSHAW,R

     

CIT180 01 Inroduc ... MW 12:30 14:00 ATHS / E235 SINGH,M

     

附表2

     

CIT160 01 Introdc ... TuTH 09:30 11:00 ATHS / E246 KREPSHAW,R

     

CIT180 25 Introdc ... MW 17:00 18:30 ATHS / E235 SINGH,M

我如何遍历XML文档?

这是我到目前为止所做的,但它不能正常工作。请帮忙。

private void LoopSchedule()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("schedule.xml");

    XmlElement root = xmlDoc.DocumentElement;
    XmlElement subRoot = (XmlElement)root.FirstChild;
    XmlNodeList children = subRoot.ChildNodes;     

    for(int i = 0; i < children.Count; i++)
    {
        XmlElement courseRoot = (XmlElement)children[i].FirstChild;
        XmlNodeList child = courseRoot.ChildNodes;

        for(int j = 0; j < child.Count; j++)
        {
            StackPanel addPanel = new StackPanel();
            Label lblChild = new Label();
            lblChild.Foreground = new SolidColorBrush(Colors.White);
            lblChild.Content = child[j].Name;

            addPanel.Children.Add(lblChild);
            stkPan_display.Children.Add(addPanel);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这里有几点:

  1. 这里需要三个循环,而不是两个循环。一个用于时间表,一个用于每个时间表中的课程,一个用于每个课程中的数据元素。
  2. 如果您想在输出中使用,则需要为计划编号创建堆栈面板和标签。你在我看到的代码中没有这样的内容。
  3. 在为课程行创建堆栈面板时,您应该在课程子项的循环外创建它,然后在循环内添加每个标签。每个课程只需要一个StackPanel,而不是每个孩子一个。
  4. 您需要将StackPanel方向设置为Horizo​​ntal为行;默认为垂直。此外,如果您将背景设置为深色,则仅将画笔颜色设置为白色,否则您将无法看到它。默认背景为白色。
  5. 请尝试使用此代码:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("schedule.xml");
    
    XmlElement root = xmlDoc.DocumentElement;  // Schedules
    
    int scheduleNumber = 0;
    foreach (XmlElement schedule in root.ChildNodes)
    {
        scheduleNumber++;
        StackPanel schedulePanel = new StackPanel { Orientation = Orientation.Horizontal };
        Label scheduleLabel = new Label { Content = "Schedule " + scheduleNumber.ToString() };
        schedulePanel.Children.Add(scheduleLabel);
        stkPan_display.Children.Add(schedulePanel);
    
        foreach (XmlElement course in schedule.ChildNodes)
        {
            StackPanel addPanel = new StackPanel { Orientation = Orientation.Horizontal };
    
            // all child elements of the Course element
            foreach (XmlElement child in course.ChildNodes)
            {
                Label lblChild = new Label();
                lblChild.Foreground = new SolidColorBrush(Colors.Black);
                lblChild.Content = child.InnerText;
                addPanel.Children.Add(lblChild);
            }
    
            stkPan_display.Children.Add(addPanel);
        }
    }
    

答案 1 :(得分:0)

使用xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var schedules = doc.Descendants("Schedule").Select(x => new {
                courses = x.Elements("Course").Select(y => new {
                    id = (string)y.Element("ID"),
                    section = (string)y.Element("Section"),
                    name = (string)y.Element("Name"),
                    days = (string)y.Element("Days"),
                    stime = ((DateTime)y.Element("STime")).TimeOfDay,
                    etime = ((DateTime)y.Element("ETime")).TimeOfDay,
                    location = (string)y.Element("Location"),
                    teacher = (string)y.Element("Teacher")
                }).ToList()
            }).ToList();

        }
    }
}