XmlTextWriter.WriteCharEntity(Char) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
強制產生指定的 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 字元。
例外狀況
字元是位在 Surrogate 字組字元範圍 0xd800
- 0xdfff
中;否則文字會產生非正確格式的 XML 文件。
WriteState 為 Closed
。
範例
下列範例會 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 字元。