我正在用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);
}
}
}
答案 0 :(得分:0)
这里有几点:
请尝试使用此代码:
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();
}
}
}