如何使用 linq to xml 解析此 XML?

发布于 2024-12-08 13:40:34 字数 1145 浏览 0 评论 0原文

我正在尝试解析以下 xml,但没有成功,没有人指导我我在这里犯了什么错误,

 string feedURL = "http://www.bbc.co.uk/arabic/index.xml";
 XDocument feedSource;
        feedSource = XDocument.Load(feedURL);

 var another = (from myFeed in feedSource.Descendants("entry")
                         select new
                         {
                             feedTitle = myFeed.Element("title").Value,
                             //feedDescription = myFeed.Element("description").Value,
                             //feedLink = myFeed.Element("link").Value,
                             feedpubDate = myFeed.Element("published") != null ? myFeed.Element("published").Value : null
                             //feedcategory = myFeed.Element("category") != null ? myFeed.Element("category").Value : null,
                             //feedItems = myFeed.Descendants("entry")
                         }
        );


            if (another != null && another.Count() > 0)
            {


            }
            else
            {
                Response.Write("No Record Found");
            }

它显示没有找到任何记录。

任何帮助将不胜感激。

i am trying to parse following xml but no sucess any one guide me what mistake am i doing here

 string feedURL = "http://www.bbc.co.uk/arabic/index.xml";
 XDocument feedSource;
        feedSource = XDocument.Load(feedURL);

 var another = (from myFeed in feedSource.Descendants("entry")
                         select new
                         {
                             feedTitle = myFeed.Element("title").Value,
                             //feedDescription = myFeed.Element("description").Value,
                             //feedLink = myFeed.Element("link").Value,
                             feedpubDate = myFeed.Element("published") != null ? myFeed.Element("published").Value : null
                             //feedcategory = myFeed.Element("category") != null ? myFeed.Element("category").Value : null,
                             //feedItems = myFeed.Descendants("entry")
                         }
        );


            if (another != null && another.Count() > 0)
            {


            }
            else
            {
                Response.Write("No Record Found");
            }

it is showing me no record found.

any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

记忆里有你的影子 2024-12-15 13:40:34

这在 LINQPad 中有效:

XNamespace xns = "http://www.w3.org/2005/Atom";
var xdoc = XDocument.Load("http://www.bbc.co.uk/arabic/index.xml");
xdoc.Element(xns + "feed").Elements(xns + "entry");

问题是缺少命名空间。

This worked in LINQPad:

XNamespace xns = "http://www.w3.org/2005/Atom";
var xdoc = XDocument.Load("http://www.bbc.co.uk/arabic/index.xml");
xdoc.Element(xns + "feed").Elements(xns + "entry");

Problem was the lacking namespace.

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