次の方法で共有


XmlTextWriter.WriteStartElement メソッド (String, String, String)

指定した開始タグを書き込み、指定した名前空間とプリフィックスに関連付けます。

Overrides Overloads Public Sub WriteStartElement( _
   ByVal prefix As String, _   ByVal localName As String, _   ByVal ns As String _)
[C#]
public override void WriteStartElement(stringprefix,stringlocalName,stringns);
[C++]
public: void WriteStartElement(String* prefix,String* localName,String* ns);
[JScript]
public override function WriteStartElement(
   prefix : String,localName : String,ns : String);

パラメータ

  • prefix
    要素の名前空間プリフィックス。
  • localName
    要素のローカル名。
  • ns
    要素に関連付ける名前空間 URI。この名前空間が既にスコープ内にあり、関連付けられたプリフィックスを持つ場合、ライタは、自動的にそのプリフィックスも書き込みます。

例外

例外の種類 条件
InvalidOperationException ライタが閉じられました。

解説

このメソッドを呼び出した後、子要素の WriteCommentWriteString 、または WriteStartElement を使用して、属性を書き込むか、内容を作成できます。 WriteEndElement を使用して要素を閉じることができます。このとき、終了タグが書き込まれます。

使用例

[Visual Basic, C#, C++] book を書き込む例を次に示します。

 
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared filename As String = "sampledata.xml"
    Public Shared Sub Main()
        
        Dim writer As New XmlTextWriter(filename, Nothing)
        'Use indenting for readability.
        writer.Formatting = Formatting.Indented
        
        writer.WriteComment("sample XML fragment")
        
        'Write an element (this one is the root).
        writer.WriteStartElement("bookstore")
        
        'Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
        
        writer.WriteStartElement("book")
        
        'Lookup the prefix and then write the ISBN attribute.
        Dim prefix As String = writer.LookupPrefix("urn:samples")
        writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
        writer.WriteString("1-861003-78")
        writer.WriteEndAttribute()
        
        'Write the title.
        writer.WriteStartElement("title")
        writer.WriteString("The Handmaid's Tale")
        writer.WriteEndElement()
        
        'Write the price.
        writer.WriteElementString("price", "19.95")
        
        'Write the style element.
        writer.WriteStartElement(prefix, "style", "urn:samples")
        writer.WriteString("hardcover")
        writer.WriteEndElement()
        
        'Write the end tag for the book element.
        writer.WriteEndElement()
        
        '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.
        Dim doc As New XmlDocument()
        'Preserve white space for readability.
        doc.PreserveWhitespace = True
        'Load the file.
        doc.Load(filename)
        
        'Write the XML content to the console.
        Console.Write(doc.InnerXml)
    End Sub 'Main 
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;

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

  public static void Main()
  {

     XmlTextWriter writer = new XmlTextWriter (filename, null);
     //Use indenting for readability.
     writer.Formatting = Formatting.Indented;
        
     writer.WriteComment("sample XML fragment");
    
     //Write an element (this one is the root).
     writer.WriteStartElement("bookstore");

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

     writer.WriteStartElement("book");

     //Lookup the prefix and then write the ISBN attribute.
     string prefix = writer.LookupPrefix("urn:samples");
     writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
     writer.WriteString("1-861003-78");
     writer.WriteEndAttribute();     

     //Write the title.
     writer.WriteStartElement("title");
     writer.WriteString("The Handmaid's Tale");
     writer.WriteEndElement();
              
     //Write the price.
     writer.WriteElementString("price", "19.95");
     
     //Write the style element.
     writer.WriteStartElement(prefix, "style", "urn:samples");
     writer.WriteString("hardcover");
     writer.WriteEndElement();

     //Write the end tag for the book element.
     writer.WriteEndElement();

     //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.
     XmlDocument doc = new XmlDocument();
     //Preserve white space for readability.
     doc.PreserveWhitespace = true;
     //Load the file
     doc.Load(filename);
    
     //Write the XML content to the console.
     Console.Write(doc.InnerXml);  

  }

}

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

int main()
{
     String* filename = S"sampledata.xml";

     XmlTextWriter* writer = new XmlTextWriter (filename, 0);
     //Use indenting for readability.
     writer->Formatting = Formatting::Indented;
        
     writer->WriteComment(S"sample XML fragment");
    
     //Write an element (this one is the root).
     writer->WriteStartElement(S"bookstore");

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

     writer->WriteStartElement(S"book");

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

     //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");
     
     //Write the style element.
     writer->WriteStartElement(prefix, S"style", S"urn:samples");
     writer->WriteString(S"hardcover");
     writer->WriteEndElement();

     //Write the end tag for the book element.
     writer->WriteEndElement();

     //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.
     XmlDocument* doc = new XmlDocument();
     //Preserve white space for readability.
     doc->PreserveWhitespace = true;
     //Load the file
     doc->Load(filename);
    
     //Write the XML content to the console.
     Console::Write(doc->InnerXml);  
}

[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 名前空間 | XmlTextWriter.WriteStartElement オーバーロードの一覧