XmlTextWriter.Formatting XmlTextWriter.Formatting XmlTextWriter.Formatting XmlTextWriter.Formatting Property

Definizione

Indica come viene formattato l'output.Indicates how the output is formatted.

public:
 property System::Xml::Formatting Formatting { System::Xml::Formatting get(); void set(System::Xml::Formatting value); };
public System.Xml.Formatting Formatting { get; set; }
member this.Formatting : System.Xml.Formatting with get, set
Public Property Formatting As Formatting

Valore della proprietà

Uno dei valori di Formatting.One of the Formatting values. Il valore predefinito è Formatting.None (nessuna formattazione speciale).The default is Formatting.None (no special formatting).

Esempi

Nell'esempio seguente viene scritto un frammento XML.The following example writes an XML fragment.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create a writer to write XML to the console.
   XmlTextWriter^ writer = nullptr;
   writer = gcnew XmlTextWriter( Console::Out );
   
   //Use indentation for readability.
   writer->Formatting = Formatting::Indented;
   writer->Indentation = 4;
   
   //Write an element (this one is the root).
   writer->WriteStartElement( "book" );
   
   //Write the title element.
   writer->WriteStartElement( "title" );
   writer->WriteString( "Pride And Prejudice" );
   writer->WriteEndElement();
   
   //Write the close tag for the root element.
   writer->WriteEndElement();
   
   //Write the XML to file and close the writer.
   writer->Close();
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  
  public static void Main()
  {
     //Create a writer to write XML to the console.
     XmlTextWriter writer = null;
     writer = new XmlTextWriter (Console.Out);

     //Use indentation for readability.
     writer.Formatting = Formatting.Indented;
     writer.Indentation = 4;
        
     //Write an element (this one is the root).
     writer.WriteStartElement("book");

     //Write the title element.
     writer.WriteStartElement("title");
     writer.WriteString("Pride And Prejudice");
     writer.WriteEndElement();

     //Write the close tag for the root element.
     writer.WriteEndElement();
             
     //Write the XML to file and close the writer.
     writer.Close();  

  }

}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create a writer to write XML to the console.
        Dim writer As XmlTextWriter = Nothing
        writer = New XmlTextWriter(Console.Out)
        
        'Use indentation for readability.
        writer.Formatting = Formatting.Indented
        writer.Indentation = 4
        
        'Write an element (this one is the root).
        writer.WriteStartElement("book")
        
        'Write the title element.
        writer.WriteStartElement("title")
        writer.WriteString("Pride And Prejudice")
        writer.WriteEndElement()
        
        'Write the close tag for the root element.
        writer.WriteEndElement()
        
        'Write the XML to file and close the writer.
        writer.Close()
    End Sub
End Class

Commenti

Nota

A partire da .NET Framework 2,0, è consigliabile creare XmlWriter istanze usando il XmlWriter.Create metodo e la XmlWriterSettings classe per sfruttare le nuove funzionalità.Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

Se l' Indented opzione è impostata, gli elementi figlio vengono rientrati Indentation usando IndentChar le proprietà e.If the Indented option is set, child elements are indented using the Indentation and IndentChar properties. Solo il contenuto dell'elemento viene rientrato.Only element content is indented. Il codice C# seguente scrive elementi HTML che includono contenuto misto:The following C# code writes out HTML elements including mixed content:

XmlTextWriter w = new XmlTextWriter(Console.Out);   
 w.Formatting = Formatting.Indented;   
 w.WriteStartElement("ol");   
 w.WriteStartElement("li");   
 w.WriteString("The big "); // This means "li" now has a mixed content model.  
 w.WriteElementString("b", "E");   
 w.WriteElementString("i", "lephant");   
 w.WriteString(" walks slowly.");   
 w.WriteEndElement();   
 w.WriteEndElement();  

Il codice precedente produce l'output seguente:The above code produces the following output:

<ol>   
  <li>The big <b>E</b><i>lephant</i> walks slowly.</li>   
</ol>  

Quando viene visualizzato in HTML, non viene visualizzato alcuno spazio vuoto tra gli elementi grassetto e corsivo.When this is viewed in HTML no white space appears between the bold and italic elements. In realtà, in questo esempio, se è stato aggiunto un rientro tra questi elementi, la parola "Elephant" verrebbe erroneamente interrotta.In fact, in this example, if indenting was added between these elements the word "Elephant" would be incorrectly broken.

Nota

La scrittura di contenuto di testo String.Empty , escluso, inserisce tale elemento in modalità contenuto misto.Writing any text content, excluding String.Empty puts that element into mixed content mode. Gli elementi figlio non ereditano lo stato della modalità "mixed".Child elements do not inherit this "mixed" mode status. Un elemento figlio di un elemento "misto" esegue il rientro, a meno che non sia incluso anche contenuto "misto".A child element of a "mixed" element does indenting, unless it is also contains "mixed" content. Il contenuto dell'elemento e il contenuto misto vengono definiti in base alle definizioni XML 1,0 di questi termini.Element content and mixed content are defined according to the XML 1.0 definitions of these terms.

Si applica a

Vedi anche