XmlTextWriter.WriteCharEntity(Char) Method

Definition

Forces the generation of a character entity for the specified Unicode character value.

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

Parameters

ch
Char

Unicode character for which to generate a character entity.

Exceptions

The character is in the surrogate pair character range, 0xd800 - 0xdfff; or the text would result in a non-well formed XML document.

Examples

The following example uses the WriteCharEntity method to write an email address.

#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

Remarks

Note

Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

This method writes the Unicode character in hexadecimal character entity reference format.

Applies to