XML Request Assertions for 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 request:
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 refer to node photos? In the PortfolioItem class, I have a property:
List<Photo> Photos {get;set;}
Any ideas would be greatly appreciated!
a source to share
I'm making some assumptions about your photoclass, assuming for the sake of this answer that you initialize the url in the constructor and the other two with their properties.
The easiest way is to just consider Photos another property to return to your linq post request and create it with a simple subquery.
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 test out the concept of closures in LINQ if you haven't already.
a source to share
Assuming 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();
a source to share
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 request photos node and fill in the photo list like this:
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();
a source to share