Query decendants in XML to Linq

Posted by Gordon on Stack Overflow See other posts from Stack Overflow or by Gordon
Published on 2010-04-21T20:30:28Z Indexed on 2010/04/21 20:33 UTC
Read the original article Hit count: 149

Filed under:
|
|

I have the following xml data:

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

In c# I am using the following link query:

List list = new List(); 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();

How do I go about querying the photos node? In the PortfolioItem class I have a property:

List Photos {get;set;}

Any ideas would be greatly appreciated!

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about c#