понедельник, 9 февраля 2009 г.

NET 3.5. Linq. Пример получения списка категорий из xml-файла системы voicecards.ru


XDocument xml = XDocument.Load(Page.MapPath("~/voicecards/xml/data.xml"));


try
{
var cards = (from x in xml.Descendants("voicecards").Elements("cards").Elements("card")
join c in xml.Descendants("voicecards").Elements("categories").Elements("category") on x.Element("catid").Value equals c.Element("catid").Value
select new
{
CardID = x.Element("cardid").Value,
CatID = int.Parse(c.Element("catid").Value),
CatName = c.Element("nameshort").Value,
CatParentID = c.Element("parentid").Value,
CatNameFull = c.Element("namefull").Value,
}
into s
group s by new { s.CatID, s.CatName, s.CatNameFull, s.CatParentID } into g
select new
{
Count=g.Count(),
g.Key.CatID,
g.Key.CatName,
g.Key.CatNameFull,
g.Key.CatParentID
}
);


}
catch { }

Комментариев нет: