2010-04-21 57 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();