SecurityElement.Escape(String) 方法

定義

將字串中無效的 XML 字元替換為其有效的 XML 等價字元。

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

參數

str
String

用來逃離無效字元的字串。

傳回

輸入字串中無效字元的輸入字串被替換。

範例

以下程式碼展示了使用此 Escape 方法將字串中無效的 XML 字元替換為其有效的 XML 對應字元。 此程式碼範例是本類別更大範例 SecurityElement 的一部分。

tagText = SecurityElement.Escape(tagText);
    tagText = SecurityElement.Escape(tagText)
End If

備註

使用此方法先替換字串中的無效字元,再使用字串。SecurityElement 若在 a SecurityElement 中使用無效字元且未成功逃脫,則會拋出 an ArgumentException

下表顯示無效的 XML 字元及其跳脫的對應字元。

無效的 XML 字元 取代為
< &lt;
> &gt;
" &quot;
' &apos;
& &amp;

適用於