XmlWriter.WriteElementString メソッド (String, String)

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

Overloads Public Sub WriteElementString( _
   ByVal localName As String, _   ByVal value As String _)
[C#]
public void WriteElementString(stringlocalName,stringvalue);
[C++]
public: void WriteElementString(String* localName,String* value);
[JScript]
public function WriteElementString(
   localName : String,value : String);

パラメータ

  • localName
    要素のローカル名。
  • value
    要素の値。

例外

例外の種類 条件
InvalidOperationException 結果が無効な XML ドキュメントになります。

使用例

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

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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

参照

XmlWriter クラス | XmlWriter メンバ | System.Xml 名前空間 | XmlWriter.WriteElementString オーバーロードの一覧