XmlConvert.VerifyXmlChars(String) 方法

定義

如果字串引數中的所有字元及 Surrogate 字組字元都是有效的 XML 字元,則傳回傳入的字串,否則擲回 XmlException,並提供遇到的第一個無效字元的相關資訊。

public:
 static System::String ^ VerifyXmlChars(System::String ^ content);
public static string VerifyXmlChars (string content);
static member VerifyXmlChars : string -> string
Public Shared Function VerifyXmlChars (content As String) As String

參數

content
String

包含要驗證之字元的 String

傳回

如果字串引數中的所有字元及 Surrogate 字組字元都是有效的 XML 字元,則為傳入的字串;否則擲回 XmlException,並提供遇到的第一個無效字元的相關資訊。

範例

下列範例會使用 VerifyXmlChars 方法來偵測 start 元素中的不合法字元。

XmlTextWriter writer3 = new XmlTextWriter("outFile.xml", null);
char illegalChar = '\uFFFE';
string charsToVerify = "Test String ";

try
{
    // Write the root element.
    writer3.WriteStartElement("root");

    // Throw an exception due illegal character.
    writer3.WriteStartElement(
        XmlConvert.VerifyXmlChars(charsToVerify + illegalChar));

    writer3.WriteString("ValueText");
    writer3.WriteEndElement();

    // Write the end tag for the root element.
    writer3.WriteEndElement();

    writer3.Close();
}
catch (XmlException e)
{
    Console.WriteLine(e.Message);
    writer3.Close();
}
Dim writer3 As XmlTextWriter = New XmlTextWriter("outFile.xml", Nothing)
Dim illegalChar As Char = ChrW(CInt("&hFFFE"))
Dim charsToVerify As String = "Test String "

Try
    ' Write the root element.
    writer3.WriteStartElement("root")

    ' Throw an exception for name that contains illegal character.
    writer3.WriteStartElement( _
        XmlConvert.VerifyXmlChars(charsToVerify + illegalChar))

    writer3.WriteString("ValueText")
    writer3.WriteEndElement()

    ' Write the end tag for the root element.
    writer3.WriteEndElement()

    writer3.Close()


Catch e As XmlException

    Console.WriteLine(e.Message)
    writer3.Close()
End Try

備註

不應該傳回傳入引數以外的其他值。 如需允許字元的詳細資訊,請參閱XML 1.0 規格 (第四版) 生產環境 [2]。 Char

如果 參數為 null, ArgumentNullException 將會擲回 。 如果有任何字元不是有效的 xml 字元, XmlException 則會擲回 ,其中會擲回第一個無效字元的相關資訊。

適用於