XmlTextWriter.WriteCharEntity(Char) 方法

定义

为指定的 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)

参数

ch
Char

为其生成字符实体的 Unicode 字符。

例外

该字符应处于代理项对字符范围 0xd800 - 0xdfff 内;否则该文本将导致格式不正确的 XML 文档。

示例

以下示例使用 WriteCharEntity 该方法编写电子邮件地址。

#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

注解

备注

从 .NET Framework 2.0 开始,我们建议使用XmlWriter.Create方法和XmlWriterSettings类来创建XmlWriter实例,以利用新功能。

此方法以十六进制字符实体引用格式写入 Unicode 字符。

适用于