XmlTextWriter.WriteString(String) Metod

Definition

Skriver det angivna textinnehållet.

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)

Parametrar

text
String

Text att skriva.

Undantag

Textsträngen innehåller ett ogiltigt surrogatpar.

Exempel

I följande exempel skrivs ett XML-fragment.

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

Kommentarer

Note

Vi rekommenderar att du skapar XmlWriter instanser med hjälp av XmlWriter.Create metoden och XmlWriterSettings klassen för att dra nytta av den nya funktionaliteten.

WriteString gör följande

  • Tecknen &, , och < ersätts med >, &amp;respektive &lt;&gt;.

  • Teckenvärden i intervallet 0x-0x1F (exklusive blankstegstecken 0x9, 0xA och 0xD) ersätts med numeriska teckenentiteter (&#0; till &#0x1Foch med ).

  • Om WriteString anropas i kontexten för ett attributvärde ersätts dubbla och enkla citattecken med &quot;&apos; respektive .

Den här indatasträngen test<item>test skrivs till exempel som test&lt;item&gt;test.

Om text är antingen null eller String.Emptyskriver den här metoden en textnod utan datainnehåll.

Gäller för