Modifier

Partager via


XmlConvert.VerifyXmlChars(String) Method

Definition

Returns the passed-in string if all the characters and surrogate pair characters in the string argument are valid XML characters, otherwise an XmlException is thrown with information on the first invalid character encountered.

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

Parameters

content
String

String that contains characters to verify.

Returns

The passed-in string if all the characters and surrogate-pair characters in the string argument are valid XML characters, otherwise an XmlException is thrown with information on the first invalid character encountered.

Examples

The following example uses the VerifyXmlChars method used to detect an illegal character in the start element.

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

Remarks

No other values than the passed in argument should be returned. See XML 1.0 spec (fourth edition) production [2] Char for details on the allowed characters.

If the parameter is null, an ArgumentNullException will be thrown. If any of the characters are not valid xml characters, an XmlException is thrown with information on the first invalid character encountered.

Applies to