XmlWriter.WriteElementString メソッド

派生クラスでオーバーライドされると、文字列値を含む要素を書き込みます。

オーバーロードの一覧

派生クラスでオーバーライドされると、指定したローカル名と値の要素を書き込みます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Sub WriteElementString(String, String)

[C#] public void WriteElementString(string, string);

[C++] public: void WriteElementString(String*, String*);

[JScript] public function WriteElementString(String, String);

派生クラスでオーバーライドされると、指定したローカル名、名前空間 URL、および値の要素を書き込みます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Sub WriteElementString(String, String, String)

[C#] public void WriteElementString(string, string, string);

[C++] public: void WriteElementString(String*, String*, String*);

[JScript] public function WriteElementString(String, String, String);

使用例

[Visual Basic, C#, C++] いくつかの書き込みメソッドを使用して XML フラグメントを作成する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、WriteElementString のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared m_Document As String = "sampledata.xml"
    
    Public Shared Sub Main()
        Dim writer As XmlTextWriter = Nothing
        Dim reader As XmlTextReader = Nothing
        
        Try
            writer = New XmlTextWriter(m_Document, Nothing)
            ' Use indenting for readability.
            writer.Formatting = Formatting.Indented
            writer.Indentation = 4
            
            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()
            
            ' Read the file back in and parse to ensure well formed XML.
            reader = New XmlTextReader(m_Document)
            Dim doc As New XmlDocument()
            ' Preserve white space for readability.
            doc.PreserveWhitespace = True
            ' Load the file.
            doc.Load(reader)
            
            ' Write the XML content to the console.
            Console.Write(doc.InnerXml)
        
        Finally
            Console.WriteLine()
            Console.WriteLine("Processing of the file {0} complete.", m_Document)
            If Not (reader Is Nothing) Then
                reader.Close()
            End If 
            If Not (writer Is Nothing) Then
                writer.Close()
            End If
        End Try
    End Sub 'Main 
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;
 
 public class Sample
 {
   private const string m_Document = "sampledata.xml";
 
   public static void Main()
   {
      XmlTextWriter writer = null;
      XmlTextReader reader = null;
 
      try
      {
        writer = new XmlTextWriter (m_Document, null);
        // Use indenting for readability.
        writer.Formatting = Formatting.Indented;
        writer.Indentation=4;
         
        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();
 
 
        // Read the file back in and parse to ensure well formed XML.
        reader = new XmlTextReader (m_Document);
        XmlDocument doc = new XmlDocument();
        // Preserve white space for readability.
        doc.PreserveWhitespace = true;
        // Load the file.
        doc.Load(reader);
     
        // Write the XML content to the console.
        Console.Write(doc.InnerXml);
      }

      finally 
      {
        Console.WriteLine();
        Console.WriteLine("Processing of the file {0} complete.", m_Document);
        if (reader != null)
           reader.Close();
        if (writer != null)
           writer.Close();
     } 
   }
 
 }

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlTextWriter* writer = 0;
   XmlTextReader* reader = 0;
   String* m_Document = S"sampledata.xml";

   try
   {
      writer = new XmlTextWriter (m_Document, 0);
      // Use indenting for readability.
      writer->Formatting = Formatting::Indented;
      writer->Indentation=4;

      writer->WriteComment(S"sample XML fragment");

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

      // Write the namespace declaration.
      writer->WriteAttributeString(S"xmlns", S"bk", 0, S"urn:samples");

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

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

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

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

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

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


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


      // Read the file back in and parse to ensure well formed XML.
      reader = new XmlTextReader (m_Document);
      XmlDocument* doc = new XmlDocument();
      // Preserve white space for readability.
      doc->PreserveWhitespace = true;
      // Load the file.
      doc->Load(reader);

      // Write the XML content to the console.
      Console::Write(doc->InnerXml);
   }

   __finally 
   {
      Console::WriteLine();
      Console::WriteLine(S"Processing of the file {0} complete.", m_Document);
      if (reader != 0)
         reader->Close();
      if (writer != 0)
         writer->Close();
   }
}

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

参照

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