C# 从XML文档中获取正确的信息
我正在尝试解析此XML文档:C# 从XML文档中获取正确的信息,c#,xml,xml-parsing,C#,Xml,Xml Parsing,我正在尝试解析此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(p
我有这门课:
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);
现在我被卡住了。如何从该文件创建剧集列表?我很困惑,因为这个赛季使用的属性
谢谢这是一个简单的教程,可能会有帮助。
它描述了如何从xml文件中获取元素
之后,您只需制作一个列表
并用数据填充即可。是一个简单的教程,可能会有所帮助。
它描述了如何从xml文件中获取元素
之后,您只需制作一个列表
并用数据填充即可。尝试以下操作:
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
});
}
var-scents=doc.SelectNodes(@“/Show/eposodelist/Season/Season”);
列表情节列表=新列表();
foreach(XmlNode片段中的片段)
{
插曲列表。添加(新插曲()
{
Season=Int32.Parse(插曲.ParentNode.Attributes[“no”].Value.ToString()),
Title=剧集。选择节点(“Title”)[0]。InnerText
});
}
试试这个:
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
});
}
var-scents=doc.SelectNodes(@“/Show/eposodelist/Season/Season”);
列表情节列表=新列表();
foreach(XmlNode片段中的片段)
{
插曲列表。添加(新插曲()
{
Season=Int32.Parse(插曲.ParentNode.Attributes[“no”].Value.ToString()),
Title=剧集。选择节点(“Title”)[0]。InnerText
});
}
试试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();
试试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();
字符串路径=@”http://services.tvrage.com/feeds/episode_list.php?sid=“+id;
IEnumerable片段=XDocument.Load(路径)
.后代(“插曲”)
.选择(x=>新剧集
{
季节=转换为16(x元素(“季节数”).值),
Title=x.元素(“Title”).值
});
字符串路径=@”http://services.tvrage.com/feeds/episode_list.php?sid=“+id;
IEnumerable片段=XDocument.Load(路径)
.后代(“插曲”)
.选择(x=>新剧集
{
季节=转换为16(x元素(“季节数”).值),
Title=x.元素(“Title”).值
});