1
<item>
 <title>Lorem Ipsum</title>
 <media:group>
   <media:content url = "http://www.sample.com" rate = "0" />
   <media:content url = "http://www.sample1.com" rate = "1" />
   <media:content url = "http://www.sample2.com" rate = "2" />
 </media:group>
</item>

我如何获得这个值:

http://www.sample2.com

我尝试使用此代码:

var query = from i in loadedData.Descendants("item")
 select new Site
 {
  Title = (string)i.Element("title"),
  Url = (string)i.Element(serv + "group").Elements(media + "content").Last()
 };
 site = query.ToList();

但 Url 的值为“”。我也有这个:

private static readonly XNamespace media = "http://www.sampleX.com/";
4

1 回答 1

0

您可以使用此 LINQ 查询:

var list = loadedData.Descendants("item")
                     .Select(e => new Site() 
                                  {
                                   Title = e.Element("title")
                                            .Value, 
                                   Url = e.Descendants(media + "content")
                                          .Last()
                                          .Attribute("url")
                                          .Value
                                  })
                     .ToList();
于 2012-07-18T16:33:50.550 回答