XmlTextWriter.Formatting プロパティ

定義

出力の書式設定方法を示します。Indicates how the output is formatted.

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 値のいずれか 1 つ。One of the Formatting values. 既定値は Formatting.None (特定の書式設定なし) です。The default is Formatting.None (no special formatting).

次の例では、XML フラグメントを書き込みます。The following example writes an XML fragment.

#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

注釈

注意

.NET Framework 2.0 以降では、新しい機能を利用するXmlWriterために、 XmlWriter.CreateメソッドとXmlWriterSettingsクラスを使用してインスタンスを作成することをお勧めします。Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

オプションが設定されIndentationている場合、子要素はプロパティIndentCharとプロパティを使用してインデントされます。 IndentedIf the Indented option is set, child elements are indented using the Indentation and IndentChar properties. 要素のコンテンツのみがインデントされます。Only element content is indented. 次C#のコードは、混合コンテンツを含む HTML 要素を書き込みます。The following C# code writes out HTML elements including mixed content:

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

上記のコードでは、次の出力が生成されます。The above code produces the following output:

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

これを HTML で表示すると、太字と斜体の要素の間に空白が表示されません。When this is viewed in HTML no white space appears between the bold and italic elements. 実際、この例では、これらの要素の間にインデントが追加されると、"象" という単語が誤って分割されます。In fact, in this example, if indenting was added between these elements the word "Elephant" would be incorrectly broken.

注意

テキストコンテンツの書き込み (をString.Empty除く)。これにより、要素は混合コンテンツモードになります。Writing any text content, excluding String.Empty puts that element into mixed content mode. 子要素は、この "mixed" モードの状態を継承しません。Child elements do not inherit this "mixed" mode status. "Mixed" 要素の子要素には、"mixed" コンテンツも含まれていない限り、インデントが行われます。A child element of a "mixed" element does indenting, unless it is also contains "mixed" content. 要素の内容混合コンテンツは、これらの用語の XML 1.0 定義に従って定義されます。Element content and mixed content are defined according to the XML 1.0 definitions of these terms.

適用対象

こちらもご覧ください