XmlTextWriter.WriteString(String) Méthode

Définition

Écrit le texte spécifié.

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)

Paramètres

text
String

Texte à écrire.

Exceptions

La chaîne de texte contient une paire de substitution non valide.

Exemples

L’exemple suivant écrit un 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

Remarques

Notes

À partir de .NET Framework 2.0, nous vous recommandons de créer XmlWriter des instances à l’aide de la XmlWriter.Create méthode et de la XmlWriterSettings classe pour tirer parti de nouvelles fonctionnalités.

WriteString effectue les opérations suivantes

  • Les caractères &, <et > sont remplacés par &amp;, &lt;et &gt;, respectivement.

  • Les valeurs de caractères de la plage 0x-0x1F (à l’exception des espaces blancs 0x9, 0xA et 0xD) sont remplacées par des entités de caractères numériques (&#0; à ).&#0x1F

  • Si WriteString est appelé dans le contexte d’une valeur d’attribut, les guillemets doubles et simples sont respectivement remplacés par &quot; et &apos; .

Par exemple, cette chaîne test<item>test d’entrée est écrite en tant que test&lt;item&gt;test.

Si text est null ou String.Empty, cette méthode écrit un nœud de texte sans contenu de données.

S’applique à