XmlWriter.WriteString(String) 方法

定义

当在派生类中被重写时,写入给定的文本内容。

public:
 abstract void WriteString(System::String ^ text);
public abstract void WriteString (string text);
public abstract void WriteString (string? text);
abstract member WriteString : string -> unit
Public MustOverride Sub WriteString (text As String)

参数

text
String

要写入的文本。

例外

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

在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”

示例

以下示例写入一个 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.
   XmlWriterSettings^ settings = gcnew XmlWriterSettings;
   settings->Indent = true;
   settings->OmitXmlDeclaration = true;
   XmlWriter^ writer = XmlWriter::Create( Console::Out, settings );
   
   // Write the book element.
   writer->WriteStartElement( L"book" );
   
   // Write the title element.
   writer->WriteStartElement( L"title" );
   writer->WriteString( L"Pride And Prejudice" );
   writer->WriteEndElement();
   
   // Write the close tag for the root element.
   writer->WriteEndElement();
   
   // Write the XML and close the writer.
   writer->Close();
   return 1;
}
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

     // Create a writer to write XML to the console.
     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     settings.OmitXmlDeclaration = true;
     XmlWriter writer = XmlWriter.Create(Console.Out, settings);

     // Write the book element.
     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 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 settings As XmlWriterSettings = new XmlWriterSettings()
     settings.Indent = true
     settings.OmitXmlDeclaration = true
     Dim writer As XmlWriter = XmlWriter.Create(Console.Out, settings)

     ' Write the book element.
     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 and close the writer.
     writer.Close()

  End Sub
End Class

注解

WriteString 执行下列操作:

  • 字符 &<> 分别替换为 &amp;&lt;&gt;

  • 使用 创建的 的默认XmlWriter行为是在尝试写入范围 0x-0x1F (不包括空格字符0x9、0xA和0xD) 时引发 ArgumentExceptionCreate 可以通过创建 XmlWriterCheckCharacters 并将 属性设置为 false的 来编写这些无效的 XML 字符。 这样做将导致字符替换为数字字符实体 (� 到 �x1F) 。 此外,默认情况下, XmlTextWriter 使用 new 运算符创建的 会将无效字符替换为数字字符实体。

注意 Microsoft 不鼓励编写无效的 XML 字符,因为许多使用 XML 的应用程序并不设计为处理无效字符。

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

例如,此输入字符串 test<item>test 写出为

test<item>test

如果 textnullString.Empty,则此方法写入不带数据内容的文本节点。

有关此方法的异步版本,请参阅 WriteStringAsync

适用于