我遇到了令人沮丧的c#问题。我有这个XML数据:
<entries>
<schedule>
<flight>UAL9858</flight>
<dep>San Francisco, CA (KSFO)</dep>
<deptime>07:00:00</deptime>
<arr>(URSS)</arr>
<arrtime>20:00:00</arrtime>
<ac>744</ac>
<dur>15:15:00</dur>
<cat>1</cat>
<id>28858</id>
</schedule>
<schedule>
<flight>UAL9857</flight>
<dep>San Francisco, CA (KSFO)</dep>
<deptime>07:00:00</deptime>
<arr>(URSS)</arr>
<arrtime>20:00:00</arrtime>
<ac>777</ac>
<dur>15:15:00</dur>
<cat>1</cat>
<id>28857</id>
</schedule>
<schedule>
<flight>SKW6289</flight>
<dep>San Francisco, CA (KSFO)</dep>
<deptime>19:38:00</deptime>
<arr>Albuquerque, NM (KABQ)</arr>
<arrtime>23:04:00</arrtime>
<ac>CRJ</ac>
<dur>02:26:00</dur>
<cat>1</cat>
<id>10413</id>
</schedule>
</entries>
现在我有一个每个节点的列(dep,deptime,arr,arrtime,ac,dur,cat,id),我想用每个节点之间的文本内容填充每一行,下面的每个条目都在它自己的专栏。这样整个xml文档以表格格式显示。我使用以下格式使用读取xml信息填充文本框(用于调试目的):
using (XmlReader reader = XmlReader.Create(fullURL))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "flight":
var flight = reader.ReadElementContentAsString();
textBox3.AppendText(flight + " | ");
break;
case "dep":
var dep = reader.ReadElementContentAsString();
textBox3.AppendText(dep + " | ");
break;
case "deptime":
var deptime = reader.ReadElementContentAsString();
textBox3.AppendText(deptime + " | ");
break;
case "arr":
var arr = reader.ReadElementContentAsString();
textBox3.AppendText(arr + " | ");
break;
case "arrtime":
var arrtime = reader.ReadElementContentAsString();
textBox3.AppendText(arrtime + " | ");
break;
case "ac":
var ac = reader.ReadElementContentAsString();
textBox3.AppendText(ac + " | ");
break;
case "dur":
var dur = reader.ReadElementContentAsString();
textBox3.AppendText(dur + " | ");
break;
case "cat":
var cat = reader.ReadElementContentAsString();
textBox3.AppendText(cat + " | ");
break;
case "id":
var id = reader.ReadElementContentAsString();
textBox3.AppendText(id + " | ");
break;
我将如何完成这项任务?
答案 0 :(得分:0)
public class Schedule{
string flight {get; set; }
string dep {get; set; }
string deptime {get; set; }
string arr {get; set; }
string arrtime {get; set; }
string ac {get; set; }
string dur {get; set; }
string cat {get; set; }
string id {get; set; }
}
public List<Schedule> Schedules = new List<Schedule>();
Schedule current;
using (XmlReader reader = XmlReader.Create(fullURL))
{
while (reader.Read())
{
current = new Schedule();
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "flight":
current.flight = reader.ReadElementContentAsString();
break;
case "dep":
current.dep = reader.ReadElementContentAsString();
break;
case "deptime":
current.deptime = reader.ReadElementContentAsString();
textBox3.AppendText(deptime + " | ");
case "arr":
current.arr = reader.ReadElementContentAsString();
break;
case "arrtime":
current.arrtime = reader.ReadElementContentAsString();
break;
case "ac":
var ac = reader.ReadElementContentAsString();
textBox3.AppendText(ac + " | ");
break;
case "dur":
current.dur = reader.ReadElementContentAsString();
break;
case "cat":
current.cat = reader.ReadElementContentAsString();
break;
case "id":
current.Id = reader.ReadElementContentAsString();
break;
Schedule.Add(current)
}
}
}
一旦填好了Schedule列表,您就可以随意显示它。