XmlWriter.WriteString(String) 方法

定義

在衍生類別中覆寫時,寫入指定的文字內容。

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

參數

text
String

要寫入的文字。

例外狀況

文字字串包含無效的 Surrogate 字組。

在先前的非同步作業完成前呼叫了 XmlWriter 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。

範例

下列範例會寫入 XML 節點。

#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" );
   writer->WriteEndElement();
   
   // Write the close tag for the root element.
   writer->WriteEndElement();
   
   // Write the XML and close the writer.
   writer->Close();
   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.
     writer.WriteStartElement("book");

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

     // Write the close tag for the root element.
     writer.WriteEndElement();

     // Write the XML and close the writer.
     writer.Close();
  }
}
Option Explicit
Option Strict

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.
     writer.WriteStartElement("book")
        
     ' Write the title element.
     writer.WriteStartElement("title")
     writer.WriteString("Pride And Prejudice")
     writer.WriteEndElement()
        
     ' Write the close tag for the root element.
     writer.WriteEndElement()
        
     ' Write the XML and close the writer.
     writer.Close()

  End Sub
End Class

備註

WriteString 會執行下列動作:

  • &字元、 <> 分別以、 &lt;&gt;取代&amp;

  • 使用 Create 建立的默認XmlWriter行為是在嘗試在範圍 0x-0x1F (中写入字符值时掷ArgumentException回 ,其中排除空格符0x9、0xA和0xD) 。 透過將 XmlWriterCheckCharacters 屬性設定 false為 ,即可撰寫這些無效的 XML 字元。 這麼做會導致字元取代為數值字元實體 (� 到 �x1F) 。 此外, XmlTextWriter 使用運算子建立的 new ,預設會將無效的字元取代為數值字元實體。

注意 Microsoft 不建議撰寫無效的 XML 字元,因為許多取用 XML 的應用程式並未設計來處理無效的字元。

  • 如果在WriteString屬性值的內容中呼叫 ,則會分別以 和 &apos; 取代&quot;雙引號和單引號。

例如,此輸入字串 test<item>test 會寫出為

test<item>test

如果 textnullString.Empty,這個方法會寫入沒有數據內容的文字節點。

如需這個方法的異步版本,請參閱 WriteStringAsync

適用於