XmlTextWriter.WriteString(String) 方法

定义

写入给定的文本内容。

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)

参数

text
String

要写入的文本。

例外

文本字符串包含无效的代理项对。

示例

以下示例写入 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

注解

备注

从 .NET Framework 2.0 开始,我们建议使用XmlWriter.Create方法和XmlWriterSettings类创建XmlWriter实例,以利用新功能。

WriteString 执行以下操作

  • 字符、字符<``&``>以及分别替换为 &amp;``&lt;、和。&gt;

  • 范围 0x-0x1F (中的字符值(不包括空格字符0x9、0xA和0xD) )将替换为通过&#0x1F) (的数字字符实体&#0;

  • 如果在 WriteString 属性值的上下文中调用,则双引号和单引号将分别替换为 &quot;&apos; 单独引用。

例如,此输入字符串 test<item>test 将写入为 test&lt;item&gt;test.

如果是text或者null``String.Empty,此方法将写入没有数据内容的文本节点。

适用于