XmlTextWriter.WriteString(String) Metoda

Definicja

Zapisuje daną zawartość tekstowa.

public:
 override void WriteString(System::String ^ text);
public override void WriteString (string? text);
public override void WriteString (string text);
override this.WriteString : string -> unit
Public Overrides Sub WriteString (text As String)

Parametry

text
String

Tekst do zapisania.

Wyjątki

Ciąg tekstowy zawiera nieprawidłową parę zastępczą.

Przykłady

Poniższy przykład zapisuje fragment 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.
   XmlTextWriter^ writer = nullptr;
   writer = gcnew XmlTextWriter( Console::Out );
   
   //Use indentation for readability.
   writer->Formatting = Formatting::Indented;
   writer->Indentation = 4;
   
   //Write an element (this one is the root).
   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 to file and close the writer.
   writer->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
     //Create a writer to write XML to the console.
     XmlTextWriter writer = null;
     writer = new XmlTextWriter (Console.Out);

     //Use indentation for readability.
     writer.Formatting = Formatting.Indented;
     writer.Indentation = 4;

     //Write an element (this one is the root).
     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 to file 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 writer As XmlTextWriter = Nothing
        writer = New XmlTextWriter(Console.Out)
        
        'Use indentation for readability.
        writer.Formatting = Formatting.Indented
        writer.Indentation = 4
        
        'Write an element (this one is the root).
        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 to file and close the writer.
        writer.Close()
    End Sub
End Class

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy tworzenie XmlWriter wystąpień przy użyciu XmlWriter.Create metody i XmlWriterSettings klasy, aby korzystać z nowych funkcji.

WriteString wykonuje następujące czynności

  • &Znaki , <i > są zastępowane odpowiednio znakami &amp;, &lt;i &gt;.

  • Wartości znaków w zakresie 0x-0x1F (z wyłączeniem białych znaków 0x9, 0xA i 0xD) są zastępowane jednostkami znaków liczbowych (&#0; przez &#0x1F).

  • Jeśli WriteString jest wywoływana w kontekście wartości atrybutu, cudzysłowy podwójne i pojedyncze są zastępowane odpowiednio i &quot;&apos; .

Na przykład ten ciąg test<item>test wejściowy jest zapisywany jako test&lt;item&gt;test.

Jeśli text ma wartość null lub String.Empty, ta metoda zapisuje węzeł tekstowy bez zawartości danych.

Dotyczy