0

我正在尝试解析这个 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);

现在我被困住了。如何从此文件创建剧集列表?我对赛季使用的属性感到困惑。

谢谢

4

4 回答 4

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();
于 2013-04-30T12:46:38.860 回答
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
    });
}
于 2013-04-30T12:45:45.500 回答
0

是一个简单的教程,它可能会有所帮助。它描述了如何从 xml 文件中获取元素。

之后,您只需要制作一个List<Episode>并用数据填充它。

于 2013-04-30T12:42:25.010 回答
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
        });
于 2013-04-30T13:03:50.520 回答