HttpUtility.HtmlDecode 方法

定義

將已以 HTML 編碼用於 HTTP 傳輸的字串轉換為解碼字串。

若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。

多載

名稱 Description
HtmlDecode(String)

將已以 HTML 編碼用於 HTTP 傳輸的字串轉換為解碼字串。

HtmlDecode(String, TextWriter)

將已編碼成 HTML 編碼的字串轉換為解碼後的字串,並將解碼後的字串傳送到 TextWriter 輸出串流。

HtmlDecode(String)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs

將已以 HTML 編碼用於 HTTP 傳輸的字串轉換為解碼字串。

public:
 static System::String ^ HtmlDecode(System::String ^ s);
public static string? HtmlDecode(string? s);
public static string HtmlDecode(string s);
static member HtmlDecode : string -> string
Public Shared Function HtmlDecode (s As String) As String

參數

s
String

要解碼的字串。

傳回

一個解碼的字串。

範例

以下程式碼範例展示了HtmlEncodeHtmlDecode類別的 和 HttpUtility 方法。 輸入字串是使用 該 HtmlEncode 方法編碼的。 接著用該 HtmlDecode 方法解碼所得到的編碼字串。

using System;
using System.Web;
using System.IO;

class MyNewClass
{
    public static void Main()
    {
        Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
        string myString = Console.ReadLine();

        // Encode the string.
        string myEncodedString = HttpUtility.HtmlEncode(myString);

        Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
        StringWriter myWriter = new StringWriter();

        // Decode the encoded string.
        HttpUtility.HtmlDecode(myEncodedString, myWriter);

        string myDecodedString = myWriter.ToString();
        Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
    }
}
Imports System.Web
Imports System.IO

Class MyNewClass
   Public Shared Sub Main()
      Dim myString As String
      Console.WriteLine("Enter a string having '&' or '""'  in it: ")
      myString = Console.ReadLine()
      Dim myEncodedString As String
      ' Encode the string.
      myEncodedString = HttpUtility.HtmlEncode(myString)
      Console.WriteLine("HTML Encoded string is " + myEncodedString)
      Dim myWriter As New StringWriter()
      ' Decode the encoded string.
      HttpUtility.HtmlDecode(myEncodedString, myWriter)
      Console.Write("Decoded string of the above encoded string is " + myWriter.ToString())
   End Sub
End Class

備註

如果在 HTTP 串流中傳遞空白和標點符號等字元,接收端可能會被誤解。 HTML 編碼將 HTML 中不允許的字元轉換為字元實體等價物;HTML 解碼則是反向編碼。 例如,當嵌入文字區塊時,字元 < 和 > 會被編碼為 &lt;&gt; 以進行 HTTP 傳輸。

若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。

另請參閱

適用於

HtmlDecode(String, TextWriter)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs

將已編碼成 HTML 編碼的字串轉換為解碼後的字串,並將解碼後的字串傳送到 TextWriter 輸出串流。

public:
 static void HtmlDecode(System::String ^ s, System::IO::TextWriter ^ output);
public static void HtmlDecode(string? s, System.IO.TextWriter output);
public static void HtmlDecode(string s, System.IO.TextWriter output);
static member HtmlDecode : string * System.IO.TextWriter -> unit
Public Shared Sub HtmlDecode (s As String, output As TextWriter)

參數

s
String

要解碼的字串。

output
TextWriter

TextWriter 連串的輸出。

備註

如果在 HTTP 串流中傳遞空白和標點符號等字元,接收端可能會被誤解。 HTML 編碼將 HTML 中不允許的字元轉換為字元實體等價物;HTML 解碼則是反向編碼。 例如,當嵌入文字區塊時,字元 < 和 > 會被編碼為 &lt;&gt; 以進行 HTTP 傳輸。

若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。

另請參閱

適用於