使用 LINQ 解析 XML 元素

发布于 2024-11-25 15:33:53 字数 1148 浏览 1 评论 0原文

我有这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-12-02 15:33:53

这些节点都位于 http://www.w3.org/2005/Atom 命名空间中。因此,您需要使用此命名空间作为查询前缀,否则查询会尝试在空命名空间中查找这些元素。

XNamespace atom = "http://www.w3.org/2005/Atom";
var list = from y in xelement.Descendants(atom+"entry")
               select new Update()
               {
                   Title = y.Element(atom+"title").Value,
                   Pubdate = y.Element(atom+"published").Value,
                   Descr = y.Element(atom+"content").Value,
                   Link = y.Element(atom+"link").Attribute("href").Value
               };

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.

XNamespace atom = "http://www.w3.org/2005/Atom";
var list = from y in xelement.Descendants(atom+"entry")
               select new Update()
               {
                   Title = y.Element(atom+"title").Value,
                   Pubdate = y.Element(atom+"published").Value,
                   Descr = y.Element(atom+"content").Value,
                   Link = y.Element(atom+"link").Attribute("href").Value
               };
℡Ms空城旧梦 2024-12-02 15:33:53

在您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文