[英]querying Atom namespace using Linq
here is my sample xml file: 这是我的示例xml文件:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<title>Title goes here</title>
<author><name>None</name></author>
<source>
<title>Adhocs Source - Adhocs Section</title>
<id>None</id>
<updated>2015-07-21T17:45:20.387248Z</updated>
</source>
<link rel="alternate" href="http://www.example.com/" />
<id>http://www.example.com/</id>
<updated>2015-07-21T17:45:20.387248Z</updated>
<published>2015-07-21T17:45:20.387248Z</published>
<summary>sample desc goes here...</summary>
<media:thumbnail
xmlns:media="http://search.yahoo.com/mrss/"
url="http://mscrmblog.net/wp-content/uploads/2013/10/iframe-loading.png" />
</entry>
</feed>
when i am trying to read this using c# Linq to XML query using below code: 当我尝试使用c#Linq使用以下代码读取此XML查询时:
XElement _atom = XElement.Load(atom);
XNamespace nsMedia = "http://search.yahoo.com/mrss/";
XNamespace nsAtom = "http://www.w3.org/2005/Atom";
var temp = (from item in _atom.Descendants(nsAtom + "entry")
select new FinalListToBeDisplayed()
{
Title = item.Element(nsAtom + "title") == null ? "" : item.Element(nsAtom + "title").Value,
Description = item.Element(nsAtom + "summary") == null ? "" : item.Element(nsAtom + "summary").Value,
PublishedDate = item.Element(nsAtom + "published") == null ? "" : item.Element(nsAtom + "published").Value,
Link = item.Element(nsAtom + "link") == null ? "" : item.Element(nsAtom + "link").Attribute("href") == null ? "" : item.Element(nsAtom + "link").Attribute("href").Value,
Image = item.Element(nsMedia + "thumbnail") == null ? "" : item.Element(nsMedia + "thumbnail").Attribute(nsAtom + "url") != null ? item.Element(nsMedia + "thumbnail").Attribute(nsAtom + "url").Value : ""
}).ToList();
Image is not capturing at any moment, ie the above code is not able to read thumbnails . 随时都无法捕获图像,即上述代码无法读取缩略图。 Please help me to understand the issue in the above code.
请帮助我了解以上代码中的问题。
Try this 尝试这个
XElement _atom = XElement.Load(atom);
XNamespace nsMedia = "http://search.yahoo.com/mrss/";
XNamespace nsAtom = "http://www.w3.org/2005/Atom";
var temp = (from item in _atom.Descendants(nsAtom + "entry")
select new
{
Title = item.Element(nsAtom + "title") == null ? "" : item.Element(nsAtom + "title").Value,
Description = item.Element(nsAtom + "summary") == null ? "" : item.Element(nsAtom + "summary").Value,
PublishedDate = item.Element(nsAtom + "published") == null ? "" : item.Element(nsAtom + "published").Value,
Link = item.Element(nsAtom + "link") == null ? "" : item.Element(nsAtom + "link").Attribute("href") == null ? "" : item.Element(nsAtom + "link").Attribute("href").Value,
Image = item.Element(nsMedia + "thumbnail") == null ? "" : item.Element(nsMedia + "thumbnail").Attribute("url") != null ? item.Element(nsMedia + "thumbnail").Attribute("url").Value : ""
}).ToList();
The issue is the attribute name for url
. 问题是
url
的属性名称。 url
does not have a namespace, but you are including the http://www.w3.org/2005/Atom
namespace as part of the name. url
没有名称空间,但是您将http://www.w3.org/2005/Atom
名称空间作为名称的一部分。 Remove this and use url
and it will work. 删除它并使用
url
,它将起作用。
I'd also comment that you can use the methods that return a sequence combined with the explicit conversions built into XElement
and XAttribute
to avoid your null checks. 我还要评论一下,您可以使用返回序列的方法以及
XElement
和XAttribute
内置的显式转换来避免空检查。 This achieves the same result but is a lot cleaner: 这样可以达到相同的结果,但是更加干净:
Title = (string)item.Element(nsAtom + "title") ?? "",
Description = (string)item.Element(nsAtom + "summary") ?? "",
PublishedDate = (string)item.Element(nsAtom + "published") ?? "",
Link = (string)item.Elements(nsAtom + "link")
.Attributes("href")
.SingleOrDefault() ?? "",
Image = (string)item.Elements(nsMedia + "thumbnail")
.Attributes("url")
.SingleOrDefault() ?? ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.