为什么只拿一个?Linq到XML C#

为什么只拿一个?Linq到XML C#,c#,xml,linq,C#,Xml,Linq,我不明白为什么我的代码只使用第一个标记而不使用其余标记 var xml = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Themes.xml")); var q = from f in xml.Descendants("themes") select new ThemesItem { Name = f.Element("theme").Element("nam

我不明白为什么我的代码只使用第一个标记而不使用其余标记

var xml = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Themes.xml"));

var q = from f in xml.Descendants("themes")
        select new ThemesItem
        {
            Name = f.Element("theme").Element("name").Value,
            Description = f.Element("theme").Element("description").Value,
            Author = f.Element("theme").Element("author").Value,
        };

return q.ToList();
ThemeItem只是一个带有公共字符串的get set 当我写出这些数据时,我使用一个中继器
感谢您的帮助:)

这是因为后代扩展方法接受xml节点的所有decentant,即“主题”。由于您的主题节点是各个主题标记的容器,因此只有一个主题标记,当您在其中使用.Element时,您将获得第一个主题标记

此代码应适用于:

var q = from f in xml.Descendants("theme")
        select new ThemesItem
        {
            Name = f.Element("name").Value,
            Description = f.Element("description").Value,
            Author = f.Element("author").Value,
        };

这是因为后代扩展方法接受xml节点的所有decentant,即“themes”。由于您的主题节点是各个主题标记的容器,因此只有一个主题标记,当您在其中使用.Element时,您将获得第一个主题标记

此代码应适用于:

var q = from f in xml.Descendants("theme")
        select new ThemesItem
        {
            Name = f.Element("name").Value,
            Description = f.Element("description").Value,
            Author = f.Element("author").Value,
        };

标准
标准主题
用户1
标准
标准
标准主题
用户2
标准

标准
标准主题
用户1
标准
标准
标准主题
用户2
标准
尝试使用XElement.Load()而不是XDocument.Load()

尝试使用XElement.Load()而不是XDocument.Load()


您能提供一些示例XML吗?您能提供一些示例XML吗?