次の方法で共有


XmlTextWriter.Formatting プロパティ

出力の書式設定方法を示します。

Public Property Formatting As Formatting
[C#]
public Formatting Formatting {get; set;}
[C++]
public: __property Formatting get_Formatting();public: __property void set_Formatting(Formatting);
[JScript]
public function get Formatting() : Formatting;public function set Formatting(Formatting);

プロパティ値

Formatting 値の 1 つ。既定値は Formatting.None (特殊な書式設定でない) です。

解説

インデント オプションを設定すると、子要素には Indentation プロパティと IndentChar プロパティを使用してインデント幅が適用されます。要素の内容だけにインデント幅が適用されます。混合コンテンツを含む HTML 要素を書き込む C# コードを次に示します。

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 で表示すると、太字要素と斜体要素の間に空白は入りません。この例では、これらの要素の間にインデントを追加すると、単語 "Elephant" が不適切な位置で分割されます。

メモ   String.Empty を含むテキストを書き込むと、その要素は混合コンテンツ モードになります。子要素は、この "混合" モード ステータスを継承しません。"混合" 要素の子要素は、子要素も "混合" コンテンツを含むのでない限り、インデント幅が適用されます。要素の内容 (http://www.w3.org/TR/1998/REC-xml-19980210\#sec-element-content) および混合コンテンツ (http://www.w3.org/TR/1998/REC-xml-19980210\#sec-mixed-content) は、これらの用語の XML 1.0 の定義に従って定義されます。

使用例

[Visual Basic, C#, C++] XML フラグメントを書き込む例を次に示します。

 
Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample

[C#] 
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();  

  }

}

[C++] 
#using <mscorlib.dll>
#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 = 0;
     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(S"book");

     //Write the title element.
     writer->WriteStartElement(S"title");
     writer->WriteString(S"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();  

}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

XmlTextWriter クラス | XmlTextWriter メンバ | System.Xml 名前空間 | Formatting