Partilhar via


XmlTextWriter.WriteCharEntity(Char) Método

Definição

Força a geração de uma entidade de caracteres para o valor especificado de caracteres Unicode.

public:
 override void WriteCharEntity(char ch);
public override void WriteCharEntity (char ch);
override this.WriteCharEntity : char -> unit
Public Overrides Sub WriteCharEntity (ch As Char)

Parâmetros

ch
Char

O caractere Unicode para a qual gerar uma entidade de caracteres.

Exceções

O caractere está dentro do intervalo de caracteres do par alternativo, 0xd800 - 0xdfff; ou então, o texto pode resultar em um documento XML malformado.

Exemplos

O exemplo a seguir usa o WriteCharEntity método para escrever um endereço de email.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlTextWriter^ writer = nullptr;
   try
   {
      writer = gcnew XmlTextWriter( Console::Out );
      
      // Write an element.
      writer->WriteStartElement( "address" );
      
      // Write an email address using entities
      // for the @ and . characters.
      writer->WriteString( "someone" );
      writer->WriteCharEntity( '@' );
      writer->WriteString( "example" );
      writer->WriteCharEntity( '.' );
      writer->WriteString( "com" );
      writer->WriteEndElement();
   }
   finally
   {
      
      // Close the writer.
      if ( writer != nullptr )
            writer->Close();
   }

}
using System;
using System.Xml;

public class Sample {

  public static void Main() {

    XmlTextWriter writer = null;

      try {

        writer = new XmlTextWriter (Console.Out);

        // Write an element.
        writer.WriteStartElement("address");

        // Write an email address using entities
        // for the @ and . characters.
        writer.WriteString("someone");
        writer.WriteCharEntity('@');
        writer.WriteString("example");
        writer.WriteCharEntity('.');
        writer.WriteString("com");
        writer.WriteEndElement();
    }

    finally {
      // Close the writer.
      if (writer != null)
        writer.Close();
    }
  }
}
Imports System.Xml

Public Class Sample 
 
    Public Shared Sub Main() 
   
        Dim writer As XmlTextWriter = Nothing

        Try 

            writer = new XmlTextWriter(Console.Out)

            ' Write an element.
            writer.WriteStartElement("address")
     
            ' Write an email address using entities
            ' for the @ and . characters.
            writer.WriteString("someone")
            writer.WriteCharEntity("@"c)
            writer.WriteString("example")
            writer.WriteCharEntity("."c)
            writer.WriteString("com")
            writer.WriteEndElement()        
 
        Finally
            ' Close the writer.
            If writer IsNot Nothing
                writer.Close()
            End If
        End Try

    End Sub
End Class

Comentários

Observação

A partir do .NET Framework 2.0, recomendamos que você crie XmlWriter instâncias usando o XmlWriter.Create método e a XmlWriterSettings classe para aproveitar a nova funcionalidade.

Esse método grava o caractere Unicode no formato de referência de entidade de caractere hexadecimal.

Aplica-se a