Validate the Element and Attribute Names

The XmlConvert class contains two methods that check the characters in a name, and verifies that the name conforms to the rules set by the W3C in the Extensible Markup Language (XML) 1.0 (Second Edition) recommendation. The methods are:

  • VerifyName
  • VerifyNCName

The VerifyName method checks the characters and verifies that the name is valid. If the name is valid, it is returned from the method. If the name is not valid, an exception is thrown.

The VerifyNCName method does the same function as VerifyName, however it takes the non-qualified name as its parameter. The following code example shows how VerifyName checks the name abc before using the name as an element name.

Try
   WriteStartElement(XmlConvert.VerifyName("abc"))
Catch e As exception
   Console.writeline("error")
End Try
[C#]
try{
WriteStartElement(XmlConvert.VerifyName("abc"));
   }
catch(exception e)
{
    Console.writeline("error");
}

To view a complete code example using these methods, see Customized XML Writer Creation.

See Also

Encode and Decode XML Element and Attribute Names and ID Values | Conversion of XML Data Types