XmlWriter.WriteString(String) XmlWriter.WriteString(String) XmlWriter.WriteString(String) XmlWriter.WriteString(String) Method


在衍生類別中覆寫時,寫入指定的文字內容。When overridden in a derived class, writes the given text content.

 abstract void WriteString(System::String ^ text);
public abstract void WriteString (string text);
abstract member WriteString : string -> unit
Public MustOverride Sub WriteString (text As String)


String String String String

要寫入的文字。The text to write.


文字字串包含無效的 Surrogate 字組。The text string contains an invalid surrogate pair.

在先前的非同步作業完成前呼叫了 XmlWriter 方法。An XmlWriter method was called before a previous asynchronous operation finished. 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."


下列範例會寫入 XML 節點。The following example writes an XML node.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
   // Create a writer to write XML to the console.
   XmlWriterSettings^ settings = gcnew XmlWriterSettings;
   settings->Indent = true;
   settings->OmitXmlDeclaration = true;
   XmlWriter^ writer = XmlWriter::Create( Console::Out, settings );
   // Write the book element.
   writer->WriteStartElement( L"book" );
   // Write the title element.
   writer->WriteStartElement( L"title" );
   writer->WriteString( L"Pride And Prejudice" );
   // Write the close tag for the root element.
   // Write the XML and close the writer.
   return 1;

using System;
using System.IO;
using System.Xml;

public class Sample {
  public static void Main() {
     // Create a writer to write XML to the console.
     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     settings.OmitXmlDeclaration = true;
     XmlWriter writer = XmlWriter.Create(Console.Out, settings);

     // Write the book element.

     // Write the title element.
     writer.WriteString("Pride And Prejudice");

     // Write the close tag for the root element.
     // Write the XML and close the writer.

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
  Public Shared Sub Main()

     ' Create a writer to write XML to the console.
     Dim settings As XmlWriterSettings = new XmlWriterSettings()
     settings.Indent = true
     settings.OmitXmlDeclaration = true
     Dim writer As XmlWriter = XmlWriter.Create(Console.Out, settings)

     ' Write the book element.
     ' Write the title element.
     writer.WriteString("Pride And Prejudice")
     ' Write the close tag for the root element.
     ' Write the XML and close the writer.

  End Sub 'Main 
End Class 'Sample


WriteString 會執行下列動作:WriteString does the following:

  • 字元&<,並>會取代&amp;&lt;,和&gt;分別。The characters &, <, and > are replaced with &amp;, &lt;, and &gt;, respectively.

  • 預設行為XmlWriter建立使用Create會擲回ArgumentException時嘗試寫入的字元值範圍從 0 x-0x1F (不含空白字元 0x9、 0xA 和 0xD) 中。The default behavior of an XmlWriter created using Create is to throw an ArgumentException when attempting to write character values in the range 0x-0x1F (excluding white space characters 0x9, 0xA, and 0xD). 可以藉由建立寫入這些無效的 XML 字元XmlWriter具有CheckCharacters屬性設定為falseThese invalid XML characters can be written by creating the XmlWriter with the CheckCharacters property set to false. 這樣會導致數字字元實體所取代的字元 (&#0; 透過 & #0x1F)。Doing so will result in the characters being replaced with numeric character entities (&#0; through &#0x1F). 此外,XmlTextWriter以建立new運算子會依預設以數字字元實體中取代無效的字元。Additionally, an XmlTextWriter created with the new operator will replace the invalid characters with numeric character entities by default.

請注意Microsoft 不會鼓勵寫入無效的 XML 字元,因為許多取用 XML 的應用程式並非設計來處理無效的字元的做法。Note Microsoft does not encourage the practice of writing invalid XML characters since many applications that consume XML are not designed to handle invalid characters.

  • 如果WriteString稱為在屬性值的內容中,以取代與單一的雙重引號&quot;&apos;分別。If WriteString is called in the context of an attribute value, double and single quotes are replaced with &quot; and &apos; respectively.

例如,在此輸入字串test<item>test寫出做為For example, this input string test<item>test is written out as


如果text可能nullString.Empty,這個方法會寫入任何資料內容的文字節點。If text is either null or String.Empty, this method writes a text node with no data content.

這個方法的非同步版本,請參閱WriteStringAsyncFor the asynchronous version of this method, see WriteStringAsync.