开发者

Query decendants in XML to Linq

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

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

List<Photo> Photos {get;set;}

Any ideas would be greatly appreciated!


I'm making some assumptions about your photo class, assuming for the sake of this answer that you would initialize url in the constructor, and the other two by their properties.

The most straightforward way is just to consider Photos one more property to return in your mail linq query, and create it with a simple sub-query.

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

A good opportunity to check out the concept of closures in LINQ if you haven't already.


Assuming that your Photo class looks like this:

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

You can do it like this:

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


Not as fast as the others but again something very similar:

If your PortfolioItem and Photo classes look like this:

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

Then query the photos node and populate the Photos List like so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜