4

編集 - 回答に基づいて変更:

さて、答えに基づいて変更したものは次のとおりです。

ここに文字列があります。

"November is Fruit's Fresh."    

ここに私がやっていることがあります:

    static string EscapeCharacters(string txt)
    {
        string encodedTxt = HttpUtility.HtmlEncode(txt);
        return HttpUtility.HtmlDecode(encodedTxt);
    }

    string _decodedTxt = EscapeCharacters("November is Fruit's Fresh.");

それが戻ったとき、私はまだ同じテキストを取得していますNovember is Fruit's Fresh.

編集終了

HttpUtility.HtmlDecodefromを使ってみたり、使ってSystem.WebみたりしSecurityElement.Escapeましたが、何も正しくエスケープしません。

そのため、次のような独自の置換メソッドを作成することになります。

    static string EscapeXMLCharacters(string txt)
    {
        string _txt = txt.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'").Replace("&#38;", "&").Replace("&#60;", "<").Replace("&#62;", ">").Replace("&#34;", "\\").Replace("&#39;", "'");
        return _txt;
    }

私の状況では機能しますが、すべてをカバーするのは難しく、私の状況では、í``(&#237;)またはのようなヨーロッパのキャラクターがいますé (&#233;)

特殊文字を処理する.Netに組み込まれたユーティリティメソッドはありますか?

4

2 に答える 2

1

tagText = SecurityElement.Escape(tagText);

http://msdn.microsoft.com/en-us/library/system.security.securityelement.escape.aspx

また

 System.Net.WebUtility.HtmlDecode(textContent);
于 2014-10-07T18:52:04.240 に答える
1

を使用HtmlEncodeして文字列をエンコードし、使用HtmlDecodeして元の値を返すことができます。

string x = "éí&";
string encoded = System.Web.HttpUtility.HtmlEncode(x);
Console.WriteLine(encoded);  //&#233;&#237;&amp;

string decoded = System.Web.HttpUtility.HtmlDecode(encoded);
Console.WriteLine(decoded);  //éí&

更新すると、文字列をデコードするだけで済みます。

String decoded = System.Web.HttpUtility.HtmlDecode("November is Fruit&#39;s Fresh.");
Console.WriteLine(decoded);   //November is Fruit's Fresh.
于 2013-11-07T15:36:42.053 に答える