XmlConvert.VerifyName(String) XmlConvert.VerifyName(String) XmlConvert.VerifyName(String) XmlConvert.VerifyName(String) Method

Definition

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

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

Parameters

name
String String String String

The name to verify.

Returns

The name, if it is a valid XML name.

Exceptions

name is not a valid XML name.

Examples

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

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlTextWriter^ writer = gcnew XmlTextWriter( "out.xml", nullptr );
   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();
}

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();
 
  }

}
Imports System
Imports System.Xml

public class Sample

  public shared sub Main()
     Dim writer as XmlTextWriter = new XmlTextWriter ("out.xml", nothing)
     Dim tag as string = "item name"
   
    try
	
     ' Write the root element.
     writer.WriteStartElement("root")

     writer.WriteStartElement(XmlConvert.VerifyName(tag))

     catch e as XmlException
        Console.WriteLine(e.Message)
        Console.WriteLine("Convert to a valid name...")
        writer.WriteStartElement(XmlConvert.EncodeName(tag))
     end try

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

     ' Write the end tag for the root element.
     writer.WriteEndElement()
 
     writer.Close()
  
  end sub
end class

Remarks

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

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

Applies to

See also