使用 LINQ 解析 XML 元素
我有这个xml。
<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="el-gr" xmlns="http://www.w3.org/2005/Atom">
<title type="text">name.gr</title>
<updated>2011-07-23</updated>
<link href="link" />
<entry>
<title type="html">Οι επιθέσεις σε σκανδιναβικές χώρες</title>
<summary type="text">Η Νορβηγία, μία από τις σκανδιναβικές χώρες, έζησε μετά από πάρα πολλά χρόνια (2006) τον τρόμο. Δείτε αναλυτικά τις τελευταίες «τυφλές» επιθέσεις τα τελευταία χρόνια:...</summary>
<published>2011-07-23T12:54:00+03:00</published>
<link href="link" />
</entry>
我想解析里面的一些元素。为此,我使用以下 LINQ 代码。
var list = from y in xelement.Descendants("entry")
select new Update()
{
Title = y.Element("title").Value,
Pubdate = y.Element("published").Value,
Descr = y.Element("content").Value,
Link = y.Element("link").Attribute("href").Value
};
但这不起作用。谁能告诉我我做错了什么?
I have this xml.
<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="el-gr" xmlns="http://www.w3.org/2005/Atom">
<title type="text">name.gr</title>
<updated>2011-07-23</updated>
<link href="link" />
<entry>
<title type="html">Οι επιθέσεις σε σκανδιναβικές χώρες</title>
<summary type="text">Η Νορβηγία, μία από τις σκανδιναβικές χώρες, έζησε μετά από πάρα πολλά χρόνια (2006) τον τρόμο. Δείτε αναλυτικά τις τελευταίες «τυφλές» επιθέσεις τα τελευταία χρόνια:...</summary>
<published>2011-07-23T12:54:00+03:00</published>
<link href="link" />
</entry>
i want to parse some of the elements inside . For that i use the following LINQ code.
var list = from y in xelement.Descendants("entry")
select new Update()
{
Title = y.Element("title").Value,
Pubdate = y.Element("published").Value,
Descr = y.Element("content").Value,
Link = y.Element("link").Attribute("href").Value
};
But it does not work. Can anyone tell me what am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些节点都位于
http://www.w3.org/2005/Atom
命名空间中。因此,您需要使用此命名空间作为查询前缀,否则查询会尝试在空命名空间中查找这些元素。The nodes are all in the
http://www.w3.org/2005/Atom
namespace. Therefore you need to prefix the queries with this namespace, otherwise the query tries to find those elements in the empty namespace.在您的 XML 示例中,缺少“feed”结束标记,并且您正在使用元素“content”,但我猜“summary”是正确的。
In your XML example, there is missing "feed" endtag and you are using an element "content", but I guess "summary" is correct.