Sdílet prostřednictvím


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

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;
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:

Doporučujeme vytvářet XmlWriter instance pomocí XmlWriter.Create metody a XmlWriterSettings třídy, abyste mohli využívat nové funkce.

Pokud je tato Indented možnost nastavená, odsadí se podřízené prvky pomocí Indentation vlastností a IndentChar vlastností. Odsadí se 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>

Při zobrazení v HTML se mezi tučným a kurzívou nezobrazí prázdné znaky. Ve skutečnosti, pokud bylo v tomto příkladu přidáno odsazení mezi tyto prvky, slovo "Elephant" by bylo nesprávně přerušeno.

Poznámka:

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

Platí pro

Viz také