2010-04-21 94 views
2

,我有以下XML数据:查询在XML decendants到LINQ

<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;}

任何想法,将不胜感激!

回答

1

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

最直接的方法是考虑照片一个更多的属性返回您的邮件linq查询,并创建一个简单的子查询。

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

假设你的照片类看起来是这样的:

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(); 
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();