Share via


XmlTextWriter.WriteString(String) Método

Definição

Grava o conteúdo de texto especificado.

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)

Parâmetros

text
String

O texto a ser gravado.

Exceções

A cadeia de caracteres contém um par alternativo inválido.

Exemplos

O exemplo a seguir grava um fragmento 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

Comentários

Observação

A partir do .NET Framework 2.0, recomendamos que você crie XmlWriter instâncias usando o XmlWriter.Create método e a XmlWriterSettings classe para aproveitar a nova funcionalidade.

WriteString faz o seguinte

  • Os caracteres &, <e > são substituídos por &amp;, &lt;e &gt;, respectivamente.

  • Os valores de caractere no intervalo de 0x-0x1F (excluindo caracteres de espaço em branco 0x9, 0xA e 0xD) são substituídos por entidades de caractere numérico (&#0; por meio &#0x1Fde ).

  • Se WriteString for chamado no contexto de um valor de atributo, as aspas duplas e simples serão substituídas &quot; por e &apos; respectivamente.

Por exemplo, essa cadeia de caracteres de test<item>test entrada é gravada como test&lt;item&gt;test.

Se text for null ou String.Empty, esse método gravará um nó de texto sem conteúdo de dados.

Aplica-se a