2010-06-07 117 views
0

我从XML加载关于感兴趣的城市的点,我的XML结构的一些信息是这样的:的LINQ to XML查询

<InterestPoint> 
     <id>2</id> 
     <name>Residencia PAC</name>   
     <images> 
      <image>C:\Pictures\Alien Places in The World\20090625-alien11.jpg</image> 
      <image>C:\Alien Places in The World\20090625-alien13.jpg</image> 
     </images> 
     <desc>blah blah blah blah</desc> 
     <Latitude>40.286458</Latitude> 
     <Longitude>-7.511921</Longitude> 
    </InterestPoint> 

遇到麻烦来检索图像的信息,我m只能得到一个图像,但在这个例子中有两个。我使用LINQ查询是:

CityPins = (from c in PointofInterest.Descendants("InterestPoint") 
         select new InterestPoint 
         { 
          // = c.Attribute("Latitude").Value, 
          //longitude = c.Attribute("Longitude").Value, 

          Name = c.Element("nome").Value, 
          LatLong = new VELatLong(double.Parse(c.Element("Latitude").Value), double.Parse(c.Element("Longitude").Value)), 
          Desc = c.Element("descricao").Value, 
          Images = (from img in c.Descendants("imagens") 
          select new POIimage 
          { 

           image = new Uri(img.Element("imagem").Value), 


          }).ToList<POIimage>(),      




         }).ToList<InterestPoint>(); 

图片是List<POIimages>其中POIimage是一个开放的领域类。

有人可以帮我解决这个问题吗?

+0

可以有多个''元素? – SLaks 2010-06-07 16:47:35

回答

2

通过编写c.Descendants("images"),您可以迭代所有<images>元素,并通过调用img.Element("imagem")获取其第一个<image>元素。

由于只有一个<images>(它恰好包含多个<image>元素),因此您只能获取一张图像。
<images>中的其他<image>元素将被忽略,因为您不对它们进行任何操作。

您需要在内部查询中调用c.Descendants("image")以获取所有<image>元素。

例如:

Images = (from img in c.Descendants("image") 
      select new POIimage { image = new Uri(img.Value) } 
     ).ToList(), 
1

试试这个(couldn t检验,没有VS编辑截至目前)。

... 
Images = (from img in c.Descendants("image")).SelectMany(new POIimage 
          { 
           image = new Uri(img.Element("imagem").Value) 
          }).ToList<POIimage>(),