XmlTextWriter.WriteString(String) メソッド

定義

指定したテキストの内容を書き込みます。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)

パラメーター

text
String

書き込むテキスト。Text to write.

例外

テキスト文字列に、無効なサロゲート ペアが含まれています。The text string contains an invalid surrogate pair.

次の例では、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

注釈

注意

.NET Framework 2.0 以降では、新しい機能を利用するXmlWriterために、 XmlWriter.CreateメソッドとXmlWriterSettingsクラスを使用してインスタンスを作成することをお勧めします。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次のことを行います。WriteString does the following

  • 、、 &および< &amp; &lt;の各文字は、それぞれ、、 &gt;およびに置き換えられます。 >The characters &, <, and > are replaced with &amp;, &lt;, and &gt;, respectively.

  • 範囲 0x-0x1f (空白文字0x9、0xa、および0xd を除く) の文字値は、(&#0;を通じて&#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).

  • 属性WriteString値のコンテキストでが呼び出された場合、二重引用符と単一引用符はそれぞれ&quot;&apos;に置き換えられます。If WriteString is called in the context of an attribute value, double and single quotes are replaced with &quot; and &apos; respectively.

たとえば、この入力文字列test<item>testは次のように書き込まれます。For example, this input string test<item>test is written as

test&lt;item&gt;test  

textまたはnull のいずれかの場合、このメソッドはデータコンテンツのないテキストノードを書き込みます。String.EmptyIf text is either null or String.Empty, this method writes a text node with no data content.

適用対象