XmlTextWriter.Formatting Vlastnost

Definice

Určuje, jak je výstup formátován.

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

Hodnota vlastnosti

Formatting

Jedna z Formatting hodnot. Výchozí hodnota je Formatting.None (bez speciálního formátování).

Příklady

Následující příklad zapíše fragment XML.

#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

Poznámky

Poznámka

Počínaje verzí .NET Framework 2.0 doporučujeme vytvářet XmlWriter instance pomocí XmlWriter.Create metody a XmlWriterSettings třídy, abyste mohli využívat nové funkce.

Indented Pokud je tato možnost nastavená, podřízené prvky se odsadí pomocí Indentation vlastností a vlastnostíIndentChar. Odsazený je pouze obsah elementu. Následující kód jazyka C# zapíše elementy HTML, včetně smíšeného obsahu:

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();  

Výše uvedený kód vytvoří následující výstup:

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

Když se zobrazí ve formátu HTML, nezobrazí se prázdné místo mezi tučnými a kurzívou prvky. Ve skutečnosti, pokud bylo v tomto příkladu přidáno odsazení mezi tyto prvky, slovo "Slon" by bylo nesprávně přerušeno.

Poznámka

Zápis libovolného textového obsahu, s výjimkou String.Empty toho, že tento prvek umístí do režimu smíšeného obsahu. Podřízené prvky nezdědí tento stav režimu smíšeného režimu. Podřízený prvek "smíšeného" prvku provádí odsazení, pokud neobsahuje také "smíšený" obsah. Obsah elementu a smíšený obsah jsou definovány podle definic XML 1.0 těchto termínů.

Platí pro

Viz také