XmlTextWriter.WriteStartDocument メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
バージョン "1.0" で XML 宣言を書き込みます。
オーバーロード
| 名前 | 説明 |
|---|---|
| WriteStartDocument() |
バージョン "1.0" で XML 宣言を書き込みます。 |
| WriteStartDocument(Boolean) |
バージョン "1.0" とスタンドアロン属性を使用して XML 宣言を書き込みます。 |
注釈
Note
新しい機能を利用するには、XmlWriter メソッドと XmlWriter.Create クラスを使用してXmlWriterSettings インスタンスを作成することをお勧めします。
WriteStartDocument()
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
バージョン "1.0" で XML 宣言を書き込みます。
public:
override void WriteStartDocument();
public override void WriteStartDocument();
override this.WriteStartDocument : unit -> unit
Public Overrides Sub WriteStartDocument ()
例外
これは、コンストラクターの後に呼び出される最初の書き込みメソッドではありません。
例
次の例では、書籍を表す XML ファイルを書き込みます。
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string filename = "sampledata.xml";
public static void Main()
{
XmlTextWriter writer = null;
writer = new XmlTextWriter (filename, null);
//Use indenting for readability.
writer.Formatting = Formatting.Indented;
//Write the XML delcaration.
writer.WriteStartDocument();
//Write the ProcessingInstruction node.
String PItext="type='text/xsl' href='book.xsl'";
writer.WriteProcessingInstruction("xml-stylesheet", PItext);
//Write the DocumentType node.
writer.WriteDocType("book", null , null, "<!ENTITY h 'hardcover'>");
//Write a Comment node.
writer.WriteComment("sample XML");
//Write a root element.
writer.WriteStartElement("book");
//Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
//Write the ISBN attribute.
writer.WriteAttributeString("ISBN", "1-8630-014");
//Write the title.
writer.WriteElementString("title", "The Handmaid's Tale");
//Write the style element.
writer.WriteStartElement("style");
writer.WriteEntityRef("h");
writer.WriteEndElement();
//Write the price.
writer.WriteElementString("price", "19.95");
//Write CDATA.
writer.WriteCData("Prices 15% off!!");
//Write the close tag for the root element.
writer.WriteEndElement();
writer.WriteEndDocument();
//Write the XML to file and close the writer.
writer.Flush();
writer.Close();
//Load the file into an XmlDocument to ensure well formed XML.
XmlDocument doc = new XmlDocument();
//Preserve white space for readability.
doc.PreserveWhitespace = true;
//Load the file.
doc.Load(filename);
//Display the XML content to the console.
Console.Write(doc.InnerXml);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Private Shared filename As String = "sampledata.xml"
Public Shared Sub Main()
Dim writer As XmlTextWriter = Nothing
writer = New XmlTextWriter(filename, Nothing)
'Use indenting for readability.
writer.Formatting = Formatting.Indented
'Write the XML delcaration.
writer.WriteStartDocument()
'Write the ProcessingInstruction node.
Dim PItext As String = "type='text/xsl' href='book.xsl'"
writer.WriteProcessingInstruction("xml-stylesheet", PItext)
'Write the DocumentType node.
writer.WriteDocType("book", Nothing, Nothing, "<!ENTITY h 'hardcover'>")
'Write a Comment node.
writer.WriteComment("sample XML")
'Write a root element.
writer.WriteStartElement("book")
'Write the genre attribute.
writer.WriteAttributeString("genre", "novel")
'Write the ISBN attribute.
writer.WriteAttributeString("ISBN", "1-8630-014")
'Write the title.
writer.WriteElementString("title", "The Handmaid's Tale")
'Write the style element.
writer.WriteStartElement("style")
writer.WriteEntityRef("h")
writer.WriteEndElement()
'Write the price.
writer.WriteElementString("price", "19.95")
'Write CDATA.
writer.WriteCData("Prices 15% off!!")
'Write the close tag for the root element.
writer.WriteEndElement()
writer.WriteEndDocument()
'Write the XML to file and close the writer.
writer.Flush()
writer.Close()
'Load the file into an XmlDocument to ensure well formed XML.
Dim doc As New XmlDocument()
'Preserve white space for readability.
doc.PreserveWhitespace = True
'Load the file.
doc.Load(filename)
'Display the XML content to the console.
Console.Write(doc.InnerXml)
End Sub
End Class
注釈
Note
新しい機能を利用するには、XmlWriter メソッドと XmlWriter.Create クラスを使用してXmlWriterSettings インスタンスを作成することをお勧めします。
ドキュメントのエンコード レベルは、ライターの実装方法によって決まります。 たとえば、Encoding コンストラクターでXmlTextWriter オブジェクトが指定されている場合、エンコード属性の値が決定されます。 このメソッドでは、スタンドアロン属性は作成されません。
WriteStartDocument呼び出されると、ライターは、記述しているものが整形式の XML ドキュメントであることを検証します。 たとえば、XML 宣言が最初のノードであり、ルート レベルの要素が 1 つしか存在しないことを確認します。 このメソッドが呼び出されない場合、ライターは XML フラグメントが書き込まれていると想定し、ルート レベルの規則を適用しません。
WriteStartDocumentが呼び出され、WriteProcessingInstruction メソッドを使用して別の XML 宣言を作成すると、例外がスローされます。
適用対象
WriteStartDocument(Boolean)
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
- ソース:
- XmlTextWriter.cs
バージョン "1.0" とスタンドアロン属性を使用して XML 宣言を書き込みます。
public:
override void WriteStartDocument(bool standalone);
public override void WriteStartDocument(bool standalone);
override this.WriteStartDocument : bool -> unit
Public Overrides Sub WriteStartDocument (standalone As Boolean)
パラメーター
- standalone
- Boolean
true場合、"standalone=yes" と書き込まれます。false場合、"standalone=no" と書き込まれます。
例外
これは、コンストラクターの後に呼び出される最初の書き込みメソッドではありません。
注釈
Note
新しい機能を利用するには、XmlWriter メソッドと XmlWriter.Create クラスを使用してXmlWriterSettings インスタンスを作成することをお勧めします。
ドキュメントのエンコード レベルは、ライターの実装方法によって決まります。 たとえば、Encoding コンストラクターでXmlTextWriter オブジェクトが指定されている場合、エンコード属性の値が決定されます。
WriteStartDocument呼び出されると、ライターは、記述しているものが整形式の XML ドキュメントであることを検証します。 たとえば、XML 宣言が最初のノードであり、ルート レベルの要素が 1 つしか存在しないことを確認します。 このメソッドが呼び出されない場合、ライターは XML フラグメントが書き込まれていると想定し、ルート レベルの規則を適用しません。
WriteStartDocumentが呼び出され、WriteProcessingInstruction メソッドを使用して別の XML 宣言を作成すると、例外がスローされます。