查询XML中的后代到Linq

时间:2010-04-21 20:30:29

标签: c# .net xml linq linq-to-xml

我有以下xml数据:

<portfolio>
  <item>
    <title>Site</title>
    <description>Site.com is a </description>
    <url>http://www.site.com</url>
    <photos>
      <photo url="http://www.site.com/site/thumbnail.png" thumbnail="true" description="Main" />
      <photo url="http://www.site.com/site/1.png" thumbnail="false" description="Main" />
    </photos>
  </item>  
</portfolio>

在c#中我使用以下链接查询:

List<PortfolioItem> list = new List<PortfolioItem>();
XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/app_data/portfolio.xml"));

list = (from portfolio in xmlDoc.Descendants("item")
       select new PortfolioItem()
       {
           Title = portfolio.Element("title").Value,
           Description = portfolio.Element("description").Value,
           Url = portfolio.Element("url").Value
       }).ToList();

如何查询照片节点?在PortfolioItem类中,我有一个属性:

List<Photo> Photos {get;set;}

任何想法都将不胜感激!

3 个答案:

答案 0 :(得分:1)

我对你的照片类做了一些假设,假设为了这个答案,你将在构造函数中初始化url,而另外两个由它们的属性初始化。

最直接的方法是在您的邮件linq查询中再考虑 Photos 一个属性,并使用简单的子查询创建它。

    list = (from portfolio in xmlDoc.Descendants("item")
            select new PortfolioItem()
           {
               Title = portfolio.Element("title").Value,
               Description = portfolio.Element("description").Value,
               Url = portfolio.Element("url").Value,
               Photos = (From P In portfilio.Element("photos").Elements("photo")
                   Select new Photo(P.Attribute("url").Value)
                       {
                           .Thumbnail = bool.Parse(P.Attribute("thumbnail").Value),
                           .Description = P.Attribute("description").Value
                       }).ToList()
           }).ToList();

如果你还没有在LINQ中查看closures概念的好机会。

答案 1 :(得分:1)

假设您的Photo类看起来像这样:

class Photo
{
    public string Url { get; set; }
    public bool Thumbnail { get; set; }
    public string Description { get; set; }
}

你可以这样做:

list = (from portfolio in xmlDoc.Descendants("item")
        select new PortfolioItem()
        {
            Title = portfolio.Element("title").Value,
            Description = portfolio.Element("description").Value,
            Url = portfolio.Element("url").Value,
            Photos = portfolio
                         .Element("photos")
                         .Elements("photo")
                         .Select(e => new Photo {
                             Description = e.Attribute("description").Value,
                             Url = e.Attribute("url").Value,
                             Thumbnail = bool.Parse(e.Attribute("thumbnail").Value),
                         }).ToList()
        }).ToList();

答案 2 :(得分:0)

不像其他人那么快,但又是非常相似的东西:

如果您的PortfolioItem和Photo类看起来像这样:

    public class PortfolioItem
    {
        public string Title { get; set; }
        public string Description { get; set; }
        public string Url { get; set; }
        public List<Photo> Photos { get; set; }
    }

    public class Photo
    {
        public string url { get; set; }
        public string thumbnail { get; set; }
        public string description { get; set; }
    }

然后查询照片节点并填充照片列表,如下所示:

    var list = (from portfolio in xmlDoc.Descendants("item")
     select new PortfolioItem()
           {
                    Title = portfolio.Element("title").Value,
                    Description = portfolio.Element("description").Value,
                    Url = portfolio.Element("url").Value,
                    Photos = portfolio.Elements("photos")
                             .Elements("photo")
                             .Select(p => new Photo {
                              url = p.Attribute("url").Value,
                              thumbnail = p.Attribute("thumbnail").Value,
                              description = p.Attribute("description").Value
                              }).ToList()
                     }).ToList();