【问题标题】:How to parse this XML using linq to xml?如何使用 linq to xml 解析这个 XML?
【发布时间】:2011-10-06 07:08:47
【问题描述】:

我正在尝试解析以下 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");
            }

它显示没有找到记录。

任何帮助将不胜感激。

【问题讨论】:

  • 我对此不太确定(并且无法在这台电脑上测试),但请尝试from myFeed in feedSource.Element("feed").Elements("entry")

标签: asp.net xml linq-to-xml


【解决方案1】:

这在 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");

问题是缺少命名空间。

【讨论】:

  • 是的,但它在 select new { } body 中给了我“对象引用未设置为对象的实例”...
猜你喜欢
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多