XmlTextWriter.WriteString(String) Method

Definition

Scrive il contenuto di testo specificato.Writes the given text content.

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

Parameters

text
String

Testo da scrivere.Text to write.

Exceptions

La stringa di testo contiene una coppia di surrogati non valida.The text string contains an invalid surrogate pair.

Examples

Nell'esempio seguente viene scritto un frammento XML.The following example writes an XML fragment.

#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

Remarks

Note

A partire da .NET Framework 2,0, è consigliabile creare istanze di XmlWriter usando il metodo XmlWriter.Create e la classe XmlWriterSettings per sfruttare le nuove funzionalità.Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

WriteString esegue le operazioni seguentiWriteString does the following

  • I caratteri &, <e > vengono sostituiti rispettivamente con &amp;, &lt;e &gt;.The characters &, <, and > are replaced with &amp;, &lt;, and &gt;, respectively.

  • I valori di carattere nell'intervallo 0x-0x1F (esclusi i caratteri di spazio vuoto 0x9, 0xA e 0xD) vengono sostituiti da entità di caratteri numerici (&#0; tramite &#0x1F).Character values in the range 0x-0x1F (excluding white space characters 0x9, 0xA, and 0xD) are replaced with numeric character entities (&#0; through &#0x1F).

  • Se WriteString viene chiamata nel contesto di un valore di attributo, le virgolette doppie e singole vengono sostituite rispettivamente con &quot; e &apos;.If WriteString is called in the context of an attribute value, double and single quotes are replaced with &quot; and &apos; respectively.

Ad esempio, questa stringa di input test<item>test viene scritta comeFor example, this input string test<item>test is written as

test&lt;item&gt;test  

Se text è null o String.Empty, questo metodo scrive un nodo di tipo text senza contenuto di dati.If text is either null or String.Empty, this method writes a text node with no data content.

Applies to