次の方法で共有


XmlConvert.VerifyName(String) メソッド

定義

W3C 勧告『Extended Markup Language』に照らし合わせて、名前が有効な名前であることを検証します。

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

パラメーター

name
String

検証対象となる名前。

戻り値

String

有効な XML 名の場合は、その名前。

例外

name が有効な XML 名ではありません。

namenull または String.Empty です。

次の例では、メソッドを VerifyName 使用して要素名を書き込みます。

#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.Xml

Public Class Sample

    Public Shared Sub Main()
        Dim writer As 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

注釈

このメソッドは、次の XmlWriter 方法でクラスと共に使用できます。

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

適用対象

こちらもご覧ください