2014-01-29 65 views
0

我得到了令人沮喪的c#問題。我有這樣的XML數據:C#使用while語句中的數據填充listview

<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,編曲,arrtime,交流,杜爾,貓,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

ASP.net,WinForms,WPF?你在這裏爲你的UI工作? –

回答

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) 
} 
} 
} 

一旦你有你的清單安排填充你可以顯示它,但是你喜歡。

+0

這將如何填充列表視圖? – AHaupright