解析Windows Phone7的问题

我正在使用Windows Phone 7开发应用程序,该应用程序显示来自特定URI的数据,但它不起作用。 我是堆栈,请帮帮我。 这是我的XML:

  info http://www.info.net Trouvez toutes les actualités en direct sur info.net ... fr      http://www.info.net 200 200   Actualités » Info News » News Régionales : Main info http://www.info.net/fr/actualite/actualites_info-news_news-regionales/my-main-info/54 Thu, 29 Dec 2011 00:22:00 +0100 <![CDATA[ My main info details : ...]]>  . . .  

我想显示一个包含以下内容的列表:

 Main info (title) http://www.info.net/uploads/content/thumbnails/2011122902313__news.jpg (description) My main info details (description) 

这是我的C#代码:

  var doc = XDocument.Load(new StringReader(e.Result)); var items = from c in doc.Descendants("item") select new RSSitem() { Title = c.Element("title").Value, Photo = c.Element("img").Attribute("src").Value, Description = c.Element("description").Value, Link = c.Element("link").Value, }; ListBoxNews.ItemsSource = items; 

标记不是XML文档的一部分,而是description元素的CDATA节点中的HTML元素。

要提取它,您需要使用HtmlAgilityPack ( NuGet上的HtmlAgilityPack )。

这是您的代码的更新版本:

(我将您的LINQ表达式转换为使用扩展方法,因为顺序代码在LINQ表达式中不能很好地工作)

 var items = doc.Descendants("item") .Select(c => { string descriptionHtml = c.Element("description").Value; HtmlDocument descriptionDoc = new HtmlDocument(); descriptionDoc.LoadHtml(descriptionHtml); HtmlNode imageNode = doc.DocumentNode.SelectSingleNode("//img[@src]"); string imageUrl = (imageNode != null) ? imageNode.GetAttributeValue("src", null) : null; // This might need further looking at, depending on your HTML string description = doc.DocumentNode.InnerText; return new RSSitem() { Title = c.Element("title").Value, Photo = imageUrl, Description = description, Link = c.Element("link").Value, }; }).ToList(); 

对于c数据节点来说就像

 XmlNode cDataNode = doc.SelectSingleNode("channel/description").ChildNodes[0]; 

string finalData = cDataNode.InnerText.Trim();