XmlTextWriter.WriteStartDocument Método

Definição

Escreve a declaração XML com a versão "1.0".

Sobrecargas

Name Description
WriteStartDocument()

Escreve a declaração XML com a versão "1.0".

WriteStartDocument(Boolean)

Escreve a declaração XML com a versão "1.0" e o atributo independente.

Observações

Note

Recomendamos que crie instâncias XmlWriter usando o método XmlWriter.Create e a classe XmlWriterSettings para aproveitar a nova funcionalidade.

WriteStartDocument()

Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs

Escreve a declaração XML com a versão "1.0".

public:
 override void WriteStartDocument();
public override void WriteStartDocument();
override this.WriteStartDocument : unit -> unit
Public Overrides Sub WriteStartDocument ()

Exceções

Este não é o primeiro método de escrita chamado a partir do construtor.

Exemplos

O exemplo seguinte escreve um ficheiro XML representando um livro.

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

Observações

Note

Recomendamos que crie instâncias XmlWriter usando o método XmlWriter.Create e a classe XmlWriterSettings para aproveitar a nova funcionalidade.

O nível de codificação do documento é determinado pela forma como o autor é implementado. Por exemplo, se um Encoding objeto for especificado no XmlTextWriter construtor, isto determina o valor do atributo de codificação. Este método não cria um atributo autónomo.

Quando WriteStartDocument é chamado, o escritor valida que o que está a escrever é um documento XML bem formado. Por exemplo, verifica se a declaração XML é o primeiro nó, se existe apenas um elemento ao nível da raiz, e assim sucessivamente. Se este método não for chamado, o autor assume que está a ser escrito um fragmento XML e não aplica regras de nível raiz.

Se WriteStartDocument foi chamado e o WriteProcessingInstruction método for usado para criar outra declaração XML, é lançada uma exceção.

Aplica-se a

WriteStartDocument(Boolean)

Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs
Origem:
XmlTextWriter.cs

Escreve a declaração XML com a versão "1.0" e o atributo independente.

public:
 override void WriteStartDocument(bool standalone);
public override void WriteStartDocument(bool standalone);
override this.WriteStartDocument : bool -> unit
Public Overrides Sub WriteStartDocument (standalone As Boolean)

Parâmetros

standalone
Boolean

Se true, escreve "standalone=yes"; se false, escreve "standalone=não".

Exceções

Este não é o primeiro método de escrita chamado a partir do construtor.

Observações

Note

Recomendamos que crie instâncias XmlWriter usando o método XmlWriter.Create e a classe XmlWriterSettings para aproveitar a nova funcionalidade.

O nível de codificação do documento é determinado pela forma como o autor é implementado. Por exemplo, se um Encoding objeto for especificado no XmlTextWriter construtor, isto determina o valor do atributo de codificação.

Quando WriteStartDocument é chamado, o escritor valida que o que está a escrever é um documento XML bem formado. Por exemplo, verifica se a declaração XML é o primeiro nó, se existe apenas um elemento ao nível da raiz, e assim sucessivamente. Se este método não for chamado, o autor assume que está a ser escrito um fragmento XML e não aplica regras de nível raiz.

Se WriteStartDocument foi chamado e o WriteProcessingInstruction método for usado para criar outra declaração XML, é lançada uma exceção.

Aplica-se a