2013-04-30 40 views
0

我試圖解析這個XML文檔:
http://services.tvrage.com/feeds/episode_list.php?sid=3332獲取正確的信息從XML文檔

我有這個類:

public class Episode { 
    public int Season { get; set; } 
    public string Title { get; set; } 
} 

我的代碼:

string path = "http://services.tvrage.com/feeds/episode_list.php?sid=" + id; 

XmlDocument doc = new XmlDocument(); 
doc.Load(path); 

現在我卡住了。我如何從這個文件創建劇集列表?由於本賽季使用的屬性,我感到困惑。

感謝

+0

XSL是你的朋友。 – 2013-04-30 12:42:27

回答

1

試試這個:

var episodes = doc.SelectNodes(@"/Show/Episodelist/Season/episode"); 
List<Episode> episodesList = new List<Episode>(); 
foreach (XmlNode episode in episodes) 
{ 
    episodesList.Add(new Episode() 
    { 
     Season = Int32.Parse(episode.ParentNode.Attributes["no"].Value.ToString()), 
     Title = episode.SelectNodes("title")[0].InnerText 
    }); 
} 
0

Here是一個簡單的教程,這可能會有幫助。 它描述瞭如何從XML文件中獲取元素。

之後,您只需製作一個List<Episode>並填入數據。

2

如何努力的LINQ到XML?

var xDoc = XDocument.Load("http://services.tvrage.com/feeds/episode_list.php?sid=3332"); 

var name = xDoc.Root.Element("name").Value; 
var episodes = xDoc.Descendants("episode") 
        .Select(e => new 
        { 
         epnum = (string)e.Element("epnum"), 
         //seasonnum = (string)e.Element("seasonnum"), 
         seasonnum = (string)e.Parent.Attribute("no"), 
         prodnum = (string)e.Element("prodnum"), 
         airdate = (string)e.Element("airdate"), 
         link = (string)e.Element("link"), 
         title = (string)e.Element("title"), 
        }) 
        .ToList(); 
+0

謝謝,但季節號碼屬於屬性,而不是元素。 – Bv202 2013-04-30 12:48:30

+0

但也在'seasonnum'標籤中。 – I4V 2013-04-30 12:49:58

+0

不是,這是另一個號碼... – Bv202 2013-04-30 12:51:04

0
string path = @"http://services.tvrage.com/feeds/episode_list.php?sid="+id; 
IEnumerable<Episode> Episodes =XDocument.Load(path) 
     .Descendants("episode") 
     .Select(x => new Episode 
     { 

      Season = Convert.ToInt16(x.Element("seasonnum").Value), 
      Title = x.Element("title").Value 
     });