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

时间:2011-10-06 07:08:47

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

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

它显示我没有找到记录。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

这适用于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");

问题是缺少名称空间。