在Silverlight WP7中删除HTML标签 [英] Removing HTML Tags in Silverlight WP7

查看:112
本文介绍了在Silverlight WP7中删除HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试从正在下载的RSS feed中删除HTML标记,并将这些条目从RSS feed中添加到listBox中.

我知道这可以用来删除HTML标记,但是请注意我将如何在代码中实现它.

Hi,

I am trying to remove HTML tags from an RSS Feed that I am downloading and adding the items from the RSS feed into a listBox.

I know that this can be used to remove HTML tags, but I am note sure how I would implement it into my code.

public string Strip(string text) 
{
     return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}


这是我的代码,我正在尝试从description元素中删除HTML标记.


Here is my code, I am trying to remove the HTML tags from the description element.

XElement xmlScan = XElement.Parse(e.Result);

listBox1.ItemsSource = from channel in xmlScan.Descendants("item")
                       select new ScanItem
                       {
                         title = channel.Element "title").Value,
                         description = "Position: " + channel.Element("description").Value
                       };

推荐答案

好吧,如果您对Strip 方法充满信心,只需替换一下:

Well, if you are confident of the Strip method, just replace this:

description = "Position: " + channel.Element("description").Value



与此:



with this:

description = "Position: " + Strip(channel.Element("description").Value)


第一个,RSS基于XML,而不是HTML .最好解析整个提要,而不要删除标签.

您试图通过RSS来为列表框创建数据源的尝试与列表框缺乏层次结构的事实相冲突.因此,您首先需要自己决定如何将树状层次结构映射到线性列表框结构. (我建议您做其他事情,例如将RSS用作更合适的TreeView的来源.)

在所有情况下,您都应该从顶部元素开始解析整个RSS字段.

如果您仍要使用ListBox并仅映射一个级别或仅一种类型的元素,则一种选择是使用System.Xml.XmlReader,这是最快的解析方法,尤其是如果您想忽略a很多数据.

另外,您可以保留使用System.Xml.Linq处理XML的方法,再次,您应该解析以System.Xml.Linq.XDocumentXElement开头的整个RSS.对于记录,您始终可以使用基于System.Xml.XmlDocument的基于DOM的解析.我会说,可能最不建议您这样做.

—SA
First, RSS is based on XML, not HTML. It''s best to parse the whole feed instead of removing the tags.

Your attempt to make a data source for a list box out of RSS is clashed with the fact that list box lacks hierarchical structure. So, you first need to decide for yourself how you want to map the tree-like hierarchical structure onto linear list box structure. (I would suggest you do something else, like making RSS a source for more adequate TreeView.)

In all cases, you should parse whole RSS field starting from the top element.

If you still want to use ListBox and want to map just the elements of one level or only of one kind, one option is to use System.Xml.XmlReader, which is the fastest method of parsing, especially good if you want to ignore a lot of data.

Alternatively, you can stay with System.Xml.Linq methods of handling XML, you, again, should parse whole RSS starting with System.Xml.Linq.XDocument, XElement. For a record, you can always use DOM-based parsing based on System.Xml.XmlDocument; I would say, probably least recommended for your purposes.

—SA


这篇关于在Silverlight WP7中删除HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆