HttpServerUtility.HtmlDecode 方法

定義

解碼已編碼以消除無效 HTML 字元的字串。

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

多載

名稱 Description
HtmlDecode(String)

解碼一個 HTML 編碼的字串,並回傳解碼後的字串。

HtmlDecode(String, TextWriter)

解碼一個 HTML 編碼的字串,並將產生的輸出傳送到輸出 TextWriter 串流。

HtmlDecode(String)

解碼一個 HTML 編碼的字串,並回傳解碼後的字串。

public:
 System::String ^ HtmlDecode(System::String ^ s);
public string HtmlDecode(string s);
member this.HtmlDecode : string -> string
Public Function HtmlDecode (s As String) As String

參數

s
String

要解碼的 HTML 字串。

傳回

解碼後的文字。

範例

以下範例包含函式 ,該函 LoadDecodedFile式可將資料從檔案解碼並複製成一個字串。

<%@ PAGE LANGUAGE = "C#" %>
 <%@ IMPORT NAMESPACE = "System.IO" %>
 
 <html xmlns="http://www.w3.org/1999/xhtml">
 <script runat ="server">
 
    String LoadDecodedFile(String file)
       {
       String DecodedString = "";
       FileStream fs = new FileStream(file, FileMode.Open);
       StreamReader r = new StreamReader(fs);
 
       // Position the file pointer at the beginning of the file.
       r.BaseStream.Seek(0, SeekOrigin.Begin);
       
       // Read the entire file into a string and decode each chunk.  
       while (r.Peek() > -1)
          DecodedString += Server.HtmlDecode(r.ReadLine());
 
       r.Close();
       return DecodedString; 
       }
 
 </script>
 <head runat="server">
 <title>HttpServerUtility.HtmlDecode Example</title>
 </head>
 <body></body>
 </html>

<%@ PAGE LANGUAGE = "VB" %>
<%@ Import Namespace="System.IO" %>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<script runat = "server">
 
   Function LoadDecodedFile(file As String) As String
      Dim DecodedString As String
      Dim fs As New FileStream(file, FileMode.Open)
      Dim r As New StreamReader(fs)
      ' Position the file pointer at the beginning of the file.
      r.BaseStream.Seek(0, SeekOrigin.Begin)
      ' Read the entire file into a string and decode each chunk.
      Do While r.Peek() > -1
         DecodedString = DecodedString & _
            Server.HtmlDecode(r.ReadLine())
      Loop
      r.Close()
      LoadDecodedFile = DecodedString
   End Function
 
</script>
<head runat="server">
    <title> HttpServerUtility.HtmlDecode Example</title>
</head>
<body></body>
</html>

備註

HTML 編碼確保文字在瀏覽器中正確顯示,而不會被瀏覽器解讀為 HTML。 例如,若文字串包含小於符號(<)或大於符號(>)的字元,瀏覽器會將這些字元解讀為HTML標籤的開括號或閉括號。 當字元以 HTML 編碼時,會被轉換成字串 &lt;&gt;,這會導致瀏覽器正確顯示小於符號和大於符號的符號。 HtmlDecode 解碼已傳送至伺服器的文字。

此方法是從 ASP.NET 應用程式在執行時存取 HttpUtility.HtmlDecode 方法的方便方式。 內部而言,此方法用於 HttpUtility.HtmlDecode 解碼字串。

在 ASP.NET 網頁的程式碼背後檔案中,透過 HttpServerUtility 屬性存取 Server 類別的實例。 在不包含程式碼後方檔案的類別中,使用 來 HttpContext.Current.Server 存取該 HttpServerUtility 類別的實例。

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

適用於

HtmlDecode(String, TextWriter)

解碼一個 HTML 編碼的字串,並將產生的輸出傳送到輸出 TextWriter 串流。

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

參數

s
String

要解碼的 HTML 字串。

output
TextWriter

包含解碼字串的 TextWriter 輸出串流。

範例

以下範例解碼了一個已以 HTML 編碼以便透過 HTTP 傳輸的字串。 它解碼包含「This is EncodedStringa Test String<.」的字串>,並將其複製到名為DecodedString「This is a <Test String>.」的字串中。

String EncodedString = "This is a &ltTest String&gt.";
StringWriter writer = new StringWriter();
Server.HtmlDecode(EncodedString, writer);
String DecodedString = writer.ToString();

Dim EncodedString As String = "This is a &ltTest String&gt."
Dim writer As New StringWriter
Server.HtmlDecode(EncodedString, writer)
Dim DecodedString As String = writer.ToString()
   

備註

HTML 編碼確保文字在瀏覽器中正確顯示,而不會被瀏覽器解讀為 HTML。 例如,若文字串包含小於符號(<)或大於符號(>)的字元,瀏覽器會將這些字元解讀為HTML標籤的開括號或閉括號。 當字元以 HTML 編碼時,會被轉換成字串 &lt;&gt;,這會導致瀏覽器正確顯示小於符號和大於符號的符號。

HtmlDecode 解碼已傳送至伺服器的文字。

HtmlDecode 是從 ASP.NET 應用程式在執行時方便存取 HttpUtility.HtmlDecode 方法的方法。 內部用 HtmlDecodeHttpUtility.HtmlDecode 來解碼字串。

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

適用於