开发者

What's the best way to fix/replace xhtml entities grabbed from source code in C#?

Essentially, I'm grabbing source code which contains entities such as & and > which I would like to replace with & and >, respectively. A simple string replace would be completely exhaustive because there are hundreds of possible entities that could show up in the source code. Is there a built-in or standard way to do it so I don't have to type up a hundred lines of string replacements?

Thanks! 开发者_StackOverflow :)


Perhaps the WebUtility.HtmlDecode Method is what you're looking for

The article gives an example using Powershell to call it

HTMLDecode Sample - Using PowerShell
<#
.SYNOPSIS
    This script encodes and decodes an HTML String
.DESCRIPTION
    This script used 
.NOTES
    File Name  : Show-HtmlCoding.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : PowerShell Version 2.0
.LINK
    This script posted to:
        http://www.pshscripts.blogspot.com
    MSDN sample posted tot:
        http://msdn.microsoft.com/en-us/library/ee388364.aspx
.EXAMPLE
    PSH [C:\foo]: .\Show-HtmlCoding.ps1
    Original String: <this is a string123> & so is this one??
    Encoded String : &lt;this is a string123&gt; &amp; so is this one??
    Decoded String : <this is a string123> & so is this one??
    Original string = Decoded string?: True   
#>

# Create string to encode/decode
$Str = "<this is a string123> & so is this one??"

# Encode String
$Encstr = [System.Net.WebUtility]::HtmlEncode($str)

# Decode String
$Decstr = [System.Net.WebUtility]::HtmlDecode($EncStr)

# Display strings
"Original String: {0}" -f $Str
"Encoded String : {0}" -f $Encstr
"Decoded String : {0}" -f $Decstr
$eq = ($str -eq $Decstr)
"Original string = Decoded string?: {0}" -f $eq 


I would use a sed console command and make a small script for that. Try to google sed 1-lines. I bet you like it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜