C# LINQXML如何忽略html代码?
我正在使用Xelement-LinqtoXML解析RSS提要中的一些内容 Rss示例:C# LINQXML如何忽略html代码?,c#,linq,windows-phone-7,C#,Linq,Windows Phone 7,我正在使用Xelement-LinqtoXML解析RSS提要中的一些内容 Rss示例: <item> <title>Waterfront Ice Skating</title> <link>http://www.eventfinder.co.nz/2011/sep/wellington/wellington-waterfront-ice-skating?utm_medium=rss</link>
<item>
<title>Waterfront Ice Skating</title>
<link>http://www.eventfinder.co.nz/2011/sep/wellington/wellington-waterfront-ice-skating?utm_medium=rss</link>
<description><p>An ice skating rink in Wellington for a limited time only!
Enjoy the magic of the New Zealand winter at an outdoor skating experience with all the fun and atmosphere of New York&#039;s Rockefeller Centre or Central Park, ...</p><p>Wellington | Friday, 30 September 2011 - Sunday, 30 October 2011</p></description>
<content:encoded><![CDATA[Today, Wellington Waterfront<br/>Wellington]]></content:encoded>
<guid isPermalink="false">108703</guid>
<pubDate>2011-09-30T10:00:00Z</pubDate>
<enclosure url="http://s1.eventfinder.co.nz/uploads/events/transformed/190501-108703-13.jpg" length="5000" type="image/jpeg"></enclosure>
</item>
海滨滑冰
http://www.eventfinder.co.nz/2011/sep/wellington/wellington-waterfront-ice-skating?utm_medium=rss
惠灵顿泛冰溜冰场,时间有限!
在户外滑冰体验中享受新西兰冬季的魅力,享受纽约和新西兰的所有乐趣和气氛#039;s洛克菲勒中心或中央公园,…/PP惠灵顿| 2011年9月30日星期五-2011年10月30日星期日/p
惠灵顿]]>
108703
2011-09-30T10:00:00Z
这一切都很好,但是description元素有很多html标记需要删除
说明:
<description><p>An ice skating rink in Wellington for a limited time only!
Enjoy the magic of the New Zealand winter at an outdoor skating experience with all the fun and atmosphere of New York&#039;s Rockefeller Centre or Central Park, ...</p><p>Wellington | Friday, 30 September 2011 - Sunday, 30 October 2011</p></description>
惠灵顿泛冰场有限时间!
在户外滑冰体验中享受新西兰冬季的魅力,享受纽约和新西兰的所有乐趣和气氛#039;s洛克菲勒中心或中央公园,…/PP惠灵顿| 2011年9月30日星期五-2011年10月30日星期日/p
有人可以帮忙吗?首先,您应该用HTML编码说明的内容。这将替换编码的
<;p>代码>至
然后,您可以使用regex:或其他一些HTML解析库删除HTML标记。首先,您应该使用HTMLDE编码描述的内容。这将替换编码的<;p>代码>至
然后,您可以使用regex:或其他一些HTML解析库删除HTML标记。如果它是RSSFeed,为什么不使用System.ServiceModel.Syndication,SyncationFeed与XML读取器结合使用将处理您的XML编码问题
using (XmlReader reader = XmlReader.Create(@"C:\\Users\\justMe\\myXml.xml"))
{
SyndicationFeed myFeed = SyndicationFeed.Load(reader);
...
}
然后按照@nemesv的建议,用正则表达式删除HTML标记,或者使用类似的方法
public static string StripHTML(this string htmlText)
{
var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
return HttpUtility.HtmlDecode(reg.Replace(htmlText, string.Empty));
}
公共静态字符串StripHTML(此字符串为htmlText)
{
var reg=new Regex(“]+>”,RegexOptions.IgnoreCase);
返回HttpUtility.HtmlDecode(reg.Replace(htmlText,string.Empty));
}
如果它是RSSFeed,为什么不使用System.ServiceModel.Syndication,SynctionFeed与XML读取器结合使用将处理XML编码的问题
using (XmlReader reader = XmlReader.Create(@"C:\\Users\\justMe\\myXml.xml"))
{
SyndicationFeed myFeed = SyndicationFeed.Load(reader);
...
}
然后按照@nemesv的建议,用正则表达式删除HTML标记,或者使用类似的方法
public static string StripHTML(this string htmlText)
{
var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
return HttpUtility.HtmlDecode(reg.Replace(htmlText, string.Empty));
}
公共静态字符串StripHTML(此字符串为htmlText)
{
var reg=new Regex(“]+>”,RegexOptions.IgnoreCase);
返回HttpUtility.HtmlDecode(reg.Replace(htmlText,string.Empty));
}
你说的“忽略html代码”是什么意思。你想只提取文本吗?@AVD是的,我想只提取文本,忽略标记。看看这个链接,“忽略html代码”是什么意思。是否只提取文本?@AVD是的,我只提取文本,忽略标记。请查看此链接-否,它是XmlEncoded,不是HtmlEncoded。只要获取XElement.Value就可以了,HtmlDecode可能会出错。不,它是XmlEncoded,而不是HtmlEncoded。只要获取XElement.Value就可以了,HTMLDE代码可能会出错。