質問

編集 - 答えに基づいて変更されました:

さて、これが答えに基づいて私が変更したものです:

これが文字列です。

"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.HtmlDecode から 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;)

特別なキャラクターの世話をするユーティリティメソッドが組み込まれていますか?

役に立ちましたか?

解決

使用できます 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.

他のヒント

tagText = secultyElement.escape(tagText);

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

また

 System.Net.WebUtility.HtmlDecode(textContent);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top