Compartir por


XmlTextWriter.Formatting Propiedad

Definición

Indica cómo se da formato a la salida.

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

Valor de propiedad

Uno de los Formatting valores. El valor predeterminado es Formatting.None (sin formato especial).

Ejemplos

En el ejemplo siguiente se escribe un fragmento 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

Comentarios

Nota:

Se recomienda crear XmlWriter instancias mediante el XmlWriter.Create método y la XmlWriterSettings clase para aprovechar las nuevas funcionalidades.

Si se establece la Indented opción, se aplica sangría a los elementos secundarios mediante las Indentation propiedades y IndentChar . Solo se aplica sangría al contenido del elemento. El siguiente código de C# escribe elementos HTML, incluido el contenido mixto:

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

El código anterior genera la siguiente salida:

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

Cuando esto se ve en HTML, no aparece ningún espacio en blanco entre los elementos negrita y cursiva. De hecho, en este ejemplo, si la sangría se agregó entre estos elementos, la palabra "Elephant" se rompería incorrectamente.

Nota:

Al escribir contenido de texto, excepto String.Empty se coloca ese elemento en modo de contenido mixto. Los elementos secundarios no heredan este estado de modo "mixto". Un elemento secundario de un elemento "mixto" aplica sangría, a menos que también contenga contenido "mixto". El contenido del elemento y el contenido mixto se definen según las definiciones XML 1.0 de estos términos.

Se aplica a

Consulte también