循环遍历XmlDocument的XML子节点
本文关键字:XML 子节点 XmlDocument 遍历 循环 | 更新日期: 2023-09-27 18:14:10
我正在用c#编写一个WPF应用程序,需要显示XML文档中的数据。下面是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>
我需要输出以下内容:
附表1CIT160 01 Introdc…th 09:30 11:00 ath/E246 KREPSHAW, R
CIT180 01介绍…MW 12:30 14:00 ath/E235 SINGH, M
计划2
CIT160 01 Introdc…th 09:30 11:00 ath/E246 KREPSHAW, R
CIT180 25 Introdc…MW 17:00 18:30 ath/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);
}
}
}
这里有几点:
- 你需要三个循环,而不是两个。一个用于时间表,一个用于每个时间表中的课程,一个用于每个课程中的数据元素。
- 如果你想在输出中显示计划编号,你需要创建一个堆栈面板和标签。你的代码中没有我看到的。
- 在为课程行创建堆栈面板时,您应该在循环外部为课程子行创建堆栈面板,然后在循环中向其添加每个标签。每个课程只需要一个StackPanel,而不是每个孩子一个。
- 你需要设置StackPanel的方向为水平的行;默认为垂直。此外,如果你已经将背景设置为深色,则只将画笔颜色设置为白色,否则你将无法看到它。默认背景为白色。
试试下面的代码:
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);
}
}
使用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();
}
}
}