我正在尝试解析这个XML文档:
http://services.tvrage.com/feeds/episode_list.php?sid=3332
我有这堂课:
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 :(得分:2)
如何尝试使用Linq To 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();
答案 1 :(得分: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
});
}
答案 2 :(得分:0)
Here是一个简单的教程,它可能会有所帮助。 它描述了如何从xml文件中获取元素。
之后,您只需制作一个List<Episode>
并填入数据。
答案 3 :(得分: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
});