次の方法で共有


XmlTextWriter.WriteString メソッド

指定したテキストの内容を書き込みます。

Overrides Public Sub WriteString( _
   ByVal text As String _)
[C#]
public override void WriteString(stringtext);
[C++]
public: void WriteString(String* text);
[JScript]
public override function WriteString(
   text : String);

パラメータ

  • text
    書き込むテキスト。

例外

例外の種類 条件
ArgumentException テキスト文字列に、無効なサロゲート ペアが含まれています。

解説

WriteString は次の内容を実行します。

  • &<> の各文字は、それぞれ &amp;&lt; 、および &gt; に置き換えられます。
  • 0x-0x1F (空白文字 0x9、0x10、および 0x13 を除く) の範囲内にある文字値は、数字エンティティ (&#0 から &#0x1F) に置き換えられます。
  • WriteString を属性値のコンテキスト内で呼び出すと、単一引用符および二重引用符は、それぞれ &quot;&apos; に置き換えられます。

たとえば、入力文字列 test<item>test は次のように書き込まれます。

test&lt;item&gt;test

text が null 参照 (Visual Basic では Nothing) または String.Empty の場合、このメソッドは、データの内容を含まないテキスト ノードを書き込みます。

使用例

[Visual Basic, C#, C++] XML フラグメントを書き込む例を次に示します。

 
Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample

[C#] 
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();  

  }

}

[C++] 
#using <mscorlib.dll>
#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 = 0;
     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(S"book");

     //Write the title element.
     writer->WriteStartElement(S"title");
     writer->WriteString(S"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();  

}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

XmlTextWriter クラス | XmlTextWriter メンバ | System.Xml 名前空間