XmlConvert.VerifyName(String) Method

Definition

Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.

C#
public static string VerifyName (string name);

Parameters

name
String

The name to verify.

Returns

The name, if it is a valid XML name.

Exceptions

name is not a valid XML name.

name is null or String.Empty.

Examples

The following example uses the VerifyName method to write an element name.

C#
using System;
using System.Xml;

public class Sample{

  public static void Main(){
     XmlTextWriter writer = new XmlTextWriter ("out.xml", null);
     string tag = "item name";

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

     writer.WriteStartElement(XmlConvert.VerifyName(tag));
     }
     catch (XmlException e){
        Console.WriteLine(e.Message);
        Console.WriteLine("Convert to a valid name...");
        writer.WriteStartElement(XmlConvert.EncodeName(tag));
     }

     writer.WriteString("hammer");
     writer.WriteEndElement();

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

     writer.Close();
  }
}

Remarks

This method can be used with the XmlWriter class in the following manner.

C#
try{
  writer.WriteStartElement(XmlConvert.VerifyName("item"),"bar");
}
catch(Exception e)
{
  Console.WriteLine("error");
}

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also