C# Linq到XML:我无法比较嵌套元素

C# Linq到XML:我无法比较嵌套元素,c#,xml,linq,C#,Xml,Linq,提前谢谢,这是一个很好的资源 我相信代码解释了它自己,但为了防止我自大,我会解释我自己 我的程序根据所选流派的下拉列表,在树状视图中列出电影。每部电影都有几个类型,因此是嵌套的类型 这是XML: <movie> <title>2012</title> <director>Roland Emmerich</director> <writtenBy> <writter>

提前谢谢,这是一个很好的资源

我相信代码解释了它自己,但为了防止我自大,我会解释我自己

我的程序根据所选流派的下拉列表,在树状视图中列出电影。每部电影都有几个类型,因此是嵌套的类型

这是XML:

   <movie>
    <title>2012</title>
    <director>Roland Emmerich</director>
    <writtenBy> 
        <writter>Roland Emmerich,</writter>
        <writter>Harald Kloser</writter>
    </writtenBy>
    <releaseDate>12-Nov-2009</releaseDate>
    <actors>
        <actor>John Cusack,</actor>
        <actor>Thandie Newton, </actor>
        <actor>Chiwetel Ejiofor</actor>
    </actors>
    <filePath>H:\2012\2012.avi</filePath>
    <picPath>~\image\2012.jpg</picPath>
    <runningTime>158 min</runningTime>
    <plot>Dr. Adrian Helmsley, part of a worldwide geophysical team investigating the effect on the earth of radiation from unprecedented solar storms, learns that the earth's core is heating up. He warns U.S. President Thomas Wilson that the crust of the earth is becoming unstable and that without proper preparations for saving a fraction of the world's population, the entire race is doomed. Meanwhile, writer Jackson Curtis stumbles on the same information. While the world's leaders race to build "arks" to escape the impending cataclysm, Curtis struggles to find a way to save his family. Meanwhile, volcanic eruptions and earthquakes of unprecedented strength wreak havoc around the world. </plot>
    <trailer>http://2012-movie-trailer.blogspot.com/</trailer>
    <genres>
        <genre>Action</genre>
        <genre>Adventure</genre>
        <genre>Drama</genre>
    </genres>
    <rated>PG-13</rated>
</movie>

2012
罗兰·艾默里奇
罗兰·埃默里奇,
罗德·科洛瑟
2009年11月12日
约翰·库萨克,
坦迪·牛顿,
奇维特尔埃吉奥弗酒店
H:\2012\2012.avi
~\image\2012.jpg
158分钟
阿德里安·赫尔姆斯利博士是一个全球地球物理小组的成员,该小组调查了前所未有的太阳风暴辐射对地球的影响,他了解到地核正在升温。他警告美国总统托马斯·威尔逊,地壳正在变得不稳定,如果不做好拯救世界一小部分人口的准备,整个种族都将走向灭亡。与此同时,作家杰克逊·柯蒂斯偶然发现了同样的信息。当世界各国领导人竞相建造“方舟”以躲避即将到来的大灾难时,柯蒂斯却在努力寻找拯救家人的方法。与此同时,空前强度的火山爆发和地震在全世界造成了严重破坏。
http://2012-movie-trailer.blogspot.com/
行动
冒险
戏剧
PG-13
代码如下:


 string selectedGenre = this.ddlGenre.SelectedItem.ToString();
            XDocument xmldoc = XDocument.Load(Server.MapPath("~/App_Data/movie.xml"));

 List<Movie> movies =
                (from movie in xmldoc.Descendants("movie")
                 // The treeView doesn't exist
                 where movie.Elements("genres").Elements("genre").ToString() == selectedGenre

  select new Movie
                {
                     Title = movie.Element("title").Value
                }).ToList();

  foreach (var movie in movies)
                {
                    TreeNode myNode = new TreeNode();
                    myNode.Text = movie.Title;
                    TreeView1.Nodes.Add(myNode);
                }

string selectedGenre=this.ddlgree.SelectedItem.ToString();
XDocument xmldoc=XDocument.Load(Server.MapPath(“~/App_Data/movie.xml”);
列出电影=
(来自xmldoc.com中的电影(“电影”)
//树视图不存在
其中movie.Elements(“流派”).Elements(“流派”).ToString()==selectedGenre
选择新电影
{
Title=movie.Element(“Title”).Value
}).ToList();
foreach(电影中的电影)
{
TreeNode myNode=新的TreeNode();
myNode.Text=movie.Title;
TreeView1.Nodes.Add(myNode);
}
将代码更改为

List<Movie> movies =
                (from movie in xmldoc.Descendants("movie")
                where movie.Elements("genres").Elements("genre").Any(e => e.Value == selectedGenre)

                select new Movie
                {
                    Title = movie.Element("title").Value
                }).ToList();
列出电影=
(来自xmldoc.com中的电影(“电影”)
其中movie.Elements(“流派”).Elements(“流派”).Any(e=>e.Value==selectedGenre)
选择新电影
{
Title=movie.Element(“Title”).Value
}).ToList();
这是因为有超过1个
类型
节点,因此您必须检查其中是否有匹配的,而不仅仅是第一个。

列出电影=
List<Movie> movies =
                (from movie in xmldoc.Descendants("movie")
                 where movie.Elements("genres")
                 .Any((e) => e.Elements("genre").ToString() == selectedGenre);
(来自xmldoc.com中的电影(“电影”) 电影元素(“类型”)的位置 .Any((e)=>e.Elements(“流派”).ToString()==selectedGenre);
很有趣,我也会试试这个!如果它能用,看起来会更光滑