Поделиться через


XmlTextWriter.Formatting Свойство

Определение

Указывает, как форматируются выходные данные.

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

Значение свойства

Одно из значений Formatting . Значение по умолчанию — Formatting.None (нет специального форматирования).

Примеры

В следующем примере записывается фрагмент 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

Комментарии

Замечание

Рекомендуется создавать XmlWriter экземпляры с помощью XmlWriter.Create метода и XmlWriterSettings класса, чтобы воспользоваться преимуществами новых функций.

Indented Если параметр задан, дочерние элементы отступаются с помощью Indentation свойств и IndentChar свойств. Отступ имеет только содержимое элемента. Следующий код C# записывает HTML-элементы, включая смешанное содержимое:

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

Приведенный выше код создает следующие выходные данные:

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

Если это отображается в HTML, пробелы между полужирными и курсивными элементами не отображаются. В этом примере, если отступ был добавлен между этими элементами, слово "Слон" будет неправильно нарушено.

Замечание

Написание любого текстового содержимого, за исключением String.Empty того, что этот элемент помещает его в смешанный режим содержимого. Дочерние элементы не наследуют это состояние смешанного режима. Дочерний элемент смешанного элемента выполняет отступ, если он также не содержит смешанное содержимое. Содержимое элементов и смешанное содержимое определяются в соответствии с определениями XML 1.0 этих терминов.

Применяется к

См. также раздел