XmlTextWriter.Formatting Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menunjukkan bagaimana output diformat.
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
Nilai Properti
Salah Formatting satu nilai. Defaultnya adalah Formatting.None (tidak ada pemformatan khusus).
Contoh
Contoh berikut menulis fragmen 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
Keterangan
Nota
Kami menyarankan agar Anda membuat XmlWriter instans dengan menggunakan XmlWriter.Create metode dan XmlWriterSettings kelas untuk memanfaatkan fungsionalitas baru.
Indented Jika opsi diatur, elemen turunan diinden menggunakan Indentation properti dan IndentChar . Hanya konten elemen yang diindentasi. Kode C# berikut menulis elemen HTML termasuk konten campuran:
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();
Kode di atas menghasilkan output berikut:
<ol>
<li>The big <b>E</b><i>lephant</i> walks slowly.</li>
</ol>
Ketika ini dilihat dalam HTML, tidak ada spasi kosong yang muncul antara elemen tebal dan miring. Bahkan, dalam contoh ini, jika inden ditambahkan di antara elemen-elemen ini kata "Gajah" akan salah rusak.
Nota
Menulis konten teks apa pun, tidak termasuk String.Empty menempatkan elemen tersebut ke dalam mode konten campuran. Elemen turunan tidak mewarisi status mode "campuran" ini. Elemen turunan dari elemen "campuran" melakukan inden, kecuali juga berisi konten "campuran".
Konten elemen dan konten campuran didefinisikan sesuai dengan definisi XML 1.0 dari istilah-istilah ini.