XmlWriter.WriteElementString メソッド

定義

文字列値を含む要素を書き込みます。

オーバーロード

名前 説明
WriteElementString(String, String)

指定したローカル名と値を持つ要素を書き込みます。

WriteElementString(String, String, String)

指定したローカル名、名前空間 URI、および値を持つ要素を書き込みます。

WriteElementString(String, String, String, String)

指定したプレフィックス、ローカル名、名前空間 URI、および値を持つ要素を書き込みます。

注釈

このメソッドの非同期バージョンについては、 WriteElementStringAsyncを参照してください。

WriteElementString(String, String)

ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs

指定したローカル名と値を持つ要素を書き込みます。

public:
 void WriteElementString(System::String ^ localName, System::String ^ value);
public void WriteElementString(string localName, string value);
public void WriteElementString(string localName, string? value);
member this.WriteElementString : string * string -> unit
Public Sub WriteElementString (localName As String, value As String)

パラメーター

localName
String

要素のローカル名。

value
String

要素の値です。

例外

localName値がnullまたは空の文字列です。

-又は-

パラメーター値が無効です。

バッファー内に有効な XML 文字である文字がありますが、出力エンコードには無効です。 たとえば、出力エンコードが ASCII の場合は、要素名と属性名に 0 ~ 127 の範囲の文字のみを使用する必要があります。 無効な文字は、このメソッドの引数、またはバッファーに書き込んでいた以前のメソッドの引数に含まれている可能性があります。 このような文字は、可能な場合は文字エンティティ参照によってエスケープされます (テキスト ノードや属性値など)。 ただし、要素名、属性名、コメント、処理命令、または CDATA セクションでは、文字エンティティ参照は使用できません。

前の非同期操作が完了する前に、 XmlWriter メソッドが呼び出されました。 この場合、"非同期操作は既に進行中です" というメッセージで InvalidOperationException がスローされます。

次の例では、いくつかの書き込みメソッドを使用して XML フラグメントを作成します。

using System;
using System.IO;
using System.Xml;

 public class Sample
 {
   private const string m_Document = "sampledata.xml";

   public static void Main() {

      XmlWriter writer = null;

      try {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        writer = XmlWriter.Create (m_Document, settings);

        writer.WriteComment("sample XML fragment");

        // Write an element (this one is the root).
        writer.WriteStartElement("book");

        // Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

        // Write the genre attribute.
        writer.WriteAttributeString("genre", "novel");

        // Write the title.
        writer.WriteStartElement("title");
        writer.WriteString("The Handmaid's Tale");
        writer.WriteEndElement();

        // Write the price.
        writer.WriteElementString("price", "19.95");

        // Lookup the prefix and write the ISBN element.
        string prefix = writer.LookupPrefix("urn:samples");
        writer.WriteStartElement(prefix, "ISBN", "urn:samples");
        writer.WriteString("1-861003-78");
        writer.WriteEndElement();

        // Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover");

        // Write the close tag for the root element.
        writer.WriteEndElement();

        // Write the XML to file and close the writer.
        writer.Flush();
        writer.Close();
      }

      finally {
        if (writer != null)
           writer.Close();
     }
   }
 }
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared m_Document As String = "sampledata.xml"
    
    Public Shared Sub Main()
        Dim writer As XmlWriter = Nothing
        
      Try

        Dim settings As XmlWriterSettings = new XmlWriterSettings()
        settings.Indent = true
        writer = XmlWriter.Create (m_Document, settings)
            
        writer.WriteComment("sample XML fragment")
            
        ' Write an element (this one is the root).
        writer.WriteStartElement("book")
            
        ' Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
            
        ' Write the genre attribute.
        writer.WriteAttributeString("genre", "novel")
            
        ' Write the title.
        writer.WriteStartElement("title")
        writer.WriteString("The Handmaid's Tale")
        writer.WriteEndElement()
            
        ' Write the price.
        writer.WriteElementString("price", "19.95")
            
        ' Lookup the prefix and write the ISBN element.
        Dim prefix As String = writer.LookupPrefix("urn:samples")
        writer.WriteStartElement(prefix, "ISBN", "urn:samples")
        writer.WriteString("1-861003-78")
        writer.WriteEndElement()
            
        ' Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover")
            
        ' Write the close tag for the root element.
        writer.WriteEndElement()
            
        ' Write the XML to file and close the writer.
        writer.Flush()
        writer.Close()
        
        Finally
            If Not (writer Is Nothing) Then
                writer.Close()
            End If
        End Try

    End Sub
End Class

注釈

このメソッドの非同期バージョンについては、 WriteElementStringAsyncを参照してください。

適用対象

WriteElementString(String, String, String)

ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs

指定したローカル名、名前空間 URI、および値を持つ要素を書き込みます。

public:
 void WriteElementString(System::String ^ localName, System::String ^ ns, System::String ^ value);
public void WriteElementString(string localName, string ns, string value);
public void WriteElementString(string localName, string? ns, string? value);
member this.WriteElementString : string * string * string -> unit
Public Sub WriteElementString (localName As String, ns As String, value As String)

パラメーター

localName
String

要素のローカル名。

ns
String

要素に関連付ける名前空間 URI。

value
String

要素の値です。

例外

localName値がnullまたは空の文字列です。

-又は-

パラメーター値が無効です。

バッファー内に有効な XML 文字である文字がありますが、出力エンコードには無効です。 たとえば、出力エンコードが ASCII の場合は、要素名と属性名に 0 ~ 127 の範囲の文字のみを使用する必要があります。 無効な文字は、このメソッドの引数、またはバッファーに書き込んでいた以前のメソッドの引数に含まれている可能性があります。 このような文字は、可能な場合は文字エンティティ参照によってエスケープされます (テキスト ノードや属性値など)。 ただし、要素名、属性名、コメント、処理命令、または CDATA セクションでは、文字エンティティ参照は使用できません。

前の非同期操作が完了する前に、 XmlWriter メソッドが呼び出されました。 この場合、"非同期操作は既に進行中です" というメッセージで InvalidOperationException がスローされます。

次の例では、いくつかの書き込みメソッドを使用して XML フラグメントを作成します。

using System;
using System.IO;
using System.Xml;

 public class Sample
 {
   private const string m_Document = "sampledata.xml";

   public static void Main() {

      XmlWriter writer = null;

      try {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        writer = XmlWriter.Create (m_Document, settings);

        writer.WriteComment("sample XML fragment");

        // Write an element (this one is the root).
        writer.WriteStartElement("book");

        // Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

        // Write the genre attribute.
        writer.WriteAttributeString("genre", "novel");

        // Write the title.
        writer.WriteStartElement("title");
        writer.WriteString("The Handmaid's Tale");
        writer.WriteEndElement();

        // Write the price.
        writer.WriteElementString("price", "19.95");

        // Lookup the prefix and write the ISBN element.
        string prefix = writer.LookupPrefix("urn:samples");
        writer.WriteStartElement(prefix, "ISBN", "urn:samples");
        writer.WriteString("1-861003-78");
        writer.WriteEndElement();

        // Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover");

        // Write the close tag for the root element.
        writer.WriteEndElement();

        // Write the XML to file and close the writer.
        writer.Flush();
        writer.Close();
      }

      finally {
        if (writer != null)
           writer.Close();
     }
   }
 }
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared m_Document As String = "sampledata.xml"
    
    Public Shared Sub Main()
        Dim writer As XmlWriter = Nothing
        
      Try

        Dim settings As XmlWriterSettings = new XmlWriterSettings()
        settings.Indent = true
        writer = XmlWriter.Create (m_Document, settings)
            
        writer.WriteComment("sample XML fragment")
            
        ' Write an element (this one is the root).
        writer.WriteStartElement("book")
            
        ' Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
            
        ' Write the genre attribute.
        writer.WriteAttributeString("genre", "novel")
            
        ' Write the title.
        writer.WriteStartElement("title")
        writer.WriteString("The Handmaid's Tale")
        writer.WriteEndElement()
            
        ' Write the price.
        writer.WriteElementString("price", "19.95")
            
        ' Lookup the prefix and write the ISBN element.
        Dim prefix As String = writer.LookupPrefix("urn:samples")
        writer.WriteStartElement(prefix, "ISBN", "urn:samples")
        writer.WriteString("1-861003-78")
        writer.WriteEndElement()
            
        ' Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover")
            
        ' Write the close tag for the root element.
        writer.WriteEndElement()
            
        ' Write the XML to file and close the writer.
        writer.Flush()
        writer.Close()
        
        Finally
            If Not (writer Is Nothing) Then
                writer.Close()
            End If
        End Try

    End Sub
End Class

注釈

このメソッドの非同期バージョンについては、 WriteElementStringAsyncを参照してください。

適用対象

WriteElementString(String, String, String, String)

ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs
ソース:
XmlWriter.cs

指定したプレフィックス、ローカル名、名前空間 URI、および値を持つ要素を書き込みます。

public:
 void WriteElementString(System::String ^ prefix, System::String ^ localName, System::String ^ ns, System::String ^ value);
public void WriteElementString(string prefix, string localName, string ns, string value);
public void WriteElementString(string? prefix, string localName, string? ns, string? value);
member this.WriteElementString : string * string * string * string -> unit
Public Sub WriteElementString (prefix As String, localName As String, ns As String, value As String)

パラメーター

prefix
String

要素のプレフィックス。

localName
String

要素のローカル名。

ns
String

要素の名前空間 URI。

value
String

要素の値です。

例外

localName値がnullまたは空の文字列です。

-又は-

パラメーター値が無効です。

バッファー内に有効な XML 文字である文字がありますが、出力エンコードには無効です。 たとえば、出力エンコードが ASCII の場合は、要素名と属性名に 0 ~ 127 の範囲の文字のみを使用する必要があります。 無効な文字は、このメソッドの引数、またはバッファーに書き込んでいた以前のメソッドの引数に含まれている可能性があります。 このような文字は、可能な場合は文字エンティティ参照によってエスケープされます (テキスト ノードや属性値など)。 ただし、要素名、属性名、コメント、処理命令、または CDATA セクションでは、文字エンティティ参照は使用できません。

前の非同期操作が完了する前に、 XmlWriter メソッドが呼び出されました。 この場合、"非同期操作は既に進行中です" というメッセージで InvalidOperationException がスローされます。

注釈

このメソッドの非同期バージョンについては、 WriteElementStringAsyncを参照してください。

適用対象