-2

This is the code that im trying with:

 var items = xdoc.Descendants("item")
                    .Select(item => new
                    {
                        MyImage = (string)item.Elements("enclosure")
                       .Select(i => i.Attribute("url").Value)
                       .SingleOrDefault()
                    })
                    .ToList();

Here is how it looks in the xml file:

<item>
<enclosure url="http://ep00.epimg.net/politica/imagenes/2013/06/22/actualidad/1371899226_298900_1371917547_miniatura_normal.jpg" length="6409" type="image/jpeg" />
</item>

All I want to do is to grab the url

Any kind of help is appreciated

4

3 回答 3

1

改变

MyImage = (string)item.Elements("enclosure")
.Select(i => i.Attribute("url").Value)
                       .SingleOrDefault()

MyImage = (string)item.Element("enclosure").Attribute("url").Value

如果您不想检查 null,您可以尝试这些 XML 扩展并将其编写为:

MyImage = item.Get("enclosure/url", "");
于 2013-06-24T16:37:52.120 回答
0

这并不难:

var result = xdoc.Descendants("item").Select(n =>
    n.Element("enclosure").Attribute("enclosure").Value);
于 2013-06-22T19:54:53.070 回答
0

如果涉及命名空间,则.Elements(name)如果不包含命名空间,则将不匹配。要测试是否是这种情况,请尝试用 替换.Elements(name)实例.Elements().Where(e => e.Name.LocalName == name)

于 2013-06-22T19:55:48.067 回答