Linq 到 XML 查询
我正在从 xml 加载一些有关城市兴趣点的信息,我的 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>
我无法检索图像信息,我只能获取一张图像,但在这些示例中有二。我使用的 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>();
Images is a List
其中 POIimage 是带有 Uri 字段的类。
有人可以帮我解决这个查询吗?
I'm loading from a xml some information about city's points of interest, and my xml structure looks like this:
<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>
I'm having trouble to retrieve the images information, I'm only able to get one image, but in these example there are two. The Linq query that i am using is:
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>();
Images is a List<POIimages>
where POIimage is a class with a Uri field.
Could someone help me fix this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过编写 c.Descendants("images"),您可以迭代所有
元素,并获取它们的第一个< /code> 元素,通过调用
img.Element("imagem")
。由于只有一个
(恰好包含多个元素),因此您只能得到一张图像。
内的其他元素将被忽略,因为您不对它们执行任何操作。
您需要在内部查询中调用 c.Descendants("image") 来获取所有
元素。
例如:
By writing
c.Descendants("images")
, you are iterating over all of the<images>
elements, and getting their first<image>
element by callingimg.Element("imagem")
.Since there is only one
<images>
(which happens to contain multiple<image>
elements), you're only getting one image.The other
<image>
elements inside of<images>
are ignored, since you don't do anything with them.You need to call
c.Descendants("image")
in the inner query to get all of the<image>
elements.For example:
试试这个(无法测试,目前还没有 VS 编辑器)。
Try this ( couldn;t test, don't have VS editor as of now).