次の方法で共有


XmlDocument.PreserveWhitespace プロパティ

空白を保存するかどうかを示す値を取得または設定します。

Public Property PreserveWhitespace As Boolean
[C#]
public bool PreserveWhitespace {get; set;}
[C++]
public: __property bool get_PreserveWhitespace();public: __property void set_PreserveWhitespace(bool);
[JScript]
public function get PreserveWhitespace() : Boolean;public function set PreserveWhitespace(Boolean);

プロパティ値

空白である場合は true 。それ以外の場合は false 。既定値は false です。

解説

このプロパティは、読み込みおよび保存プロセス中の空白の処理方法を決定します。

Load または LoadXml が呼び出される前に PreserveWhitespacetrue の場合、空白ノードは保存されます。このプロパティが false の場合、有意な空白は保存されますが、空白は保存されません。

Save が呼び出される前に PreserveWhitespacetrue の場合、ドキュメント内の空白は出力で保存されます。このプロパティが false の場合、 XmlDocument は出力に自動的にインデントを設定します。

このメソッドは、ドキュメント オブジェクト モデル (DOM: Document Object Model) に対する Microsoft 拡張機能です。

使用例

[Visual Basic, C#, C++] ファイルから空白を取り除く方法を次の例に示します。

 
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    'Load XML data which includes white space, but ignore
    'any white space in the file.
    Dim doc as XmlDocument = new XmlDocument()
    doc.PreserveWhitespace = false
    doc.Load("book.xml")

    'Save the document as is (no white space).
    Console.WriteLine("Display the modified XML...")
    doc.PreserveWhitespace = true
    doc.Save(Console.Out)

  end sub
end class

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    //Load XML data which includes white space, but ignore
    //any white space in the file.
    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = false;
    doc.Load("book.xml");

    //Save the document as is (no white space).
    Console.WriteLine("Display the modified XML...");
    doc.PreserveWhitespace = true;
    doc.Save(Console.Out);

   }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Load XML data which includes white space, but ignore
    //any white space in the file.
    XmlDocument* doc = new XmlDocument();
    doc->PreserveWhitespace = false;
    doc->Load(S"book.xml");

    //Save the document as is (no white space).
    Console::WriteLine(S"Display the modified XML...");
    doc->PreserveWhitespace = true;
    doc->Save(Console::Out);
}

[Visual Basic, C#, C++] この例では、入力として、 book.xml というファイルを使用しています。

<!--sample XML fragment-->
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
  <title>The Handmaid's Tale</title>
  <price>14.95</price>
</book>

[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

参照

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