XmlTextWriter.WriteCharEntity Method (Char)

 

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

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public override void WriteCharEntity(
	char ch
)

Parameters

ch
Type: System.Char

Unicode character for which to generate a character entity.

Exception Condition
ArgumentException

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

InvalidOperationException

The WriteState is Closed.

System_CAPS_noteNote

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.

The following example uses the WriteCharEntity method to write an e-mail address.

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

.NET Framework
Available since 1.1
Return to top
Show: