Csharp/C#教程:解析Windows Phone7的问题分享


解析Windows Phone7的问题

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

  info https://www.info.net Trouvez toutes les actualités en direct sur info.net ... fr      https://www.info.net 200 200   Actualités » Info News » News Régionales : Main info https://www.info.net/fr/actualite/actualites_info-news_news-regionales/my-main-info/54 Thu, 29 Dec 2011 00:22:00 +0100 <![CDATA[Csharp/C#教程:解析Windows Phone7的问题分享 My main info details : ...]]>  . . .  

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

 Main info (title) https://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();

上述就是C#学习教程:解析Windows Phone7的问题分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1009403.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐