次の方法で共有


XmlDocument.Save メソッド (XmlWriter)

指定した XmlWriter に XML ドキュメントを保存します。

Overloads Public Overridable Sub Save( _
   ByVal w As XmlWriter _)
[C#]
public virtual void Save(XmlWriterw);
[C++]
public: virtual void Save(XmlWriter* w);
[JScript]
public function Save(
   w : XmlWriter);

パラメータ

  • w
    保存先の XmlWriter

例外

例外の種類 条件
XmlException 操作の結果、整形式の XML ドキュメントにならない場合があります (ドキュメント要素がない、XML 宣言の重複など)。

解説

空白は PreserveWhitespacetrue に設定されている場合だけ保存されます。

書き込まれるエンコーディングは、 XmlWriter のエンコーディングによって決定されます。XmlDeclaration ノードのエンコーディングは、 XmlWriter のエンコーディングによっても置換されます。 XmlWriter でエンコーディングが指定されていなかった場合、 XmlDocument はエンコーディング属性なしで保存されます。

ドキュメントが保存されるときに、xmlns 属性が生成され、ノード ID (LocalName + NamespaceURI) を正確に永続化します。たとえば、次に示す C# コードは、

XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("item","urn:1"));
doc.Save(Console.Out);

次の xmls 属性を生成します。

<item xmls="urn:1"/>

このメソッドは、DOM (Document Object Model) に対する Microsoft 拡張機能です。

使用例

[Visual Basic, C#, C++] XML を XmlDocument オブジェクトに読み込み、ファイルに保存する例を次に示します。

 
Imports System
Imports System.Xml

public class Sample 

  public shared sub Main() 
 
    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<item><name>wrench</name></item>")

   ' Add a price element.
   Dim newElem as XmlElement = doc.CreateElement("price")
   newElem.InnerText = "10.95"
   doc.DocumentElement.AppendChild(newElem)

    ' Save the document to a file and auto-indent the output.
    Dim writer as XmlTextWriter = new XmlTextWriter("data.xml",nothing)
    writer.Formatting = Formatting.Indented
    doc.Save(writer)
  end sub
end class

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

public class Sample {

  public static void Main() {
 
    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");

   // Add a price element.
   XmlElement newElem = doc.CreateElement("price");
   newElem.InnerText = "10.95";
   doc.DocumentElement.AppendChild(newElem);

    // Save the document to a file and auto-indent the output.
    XmlTextWriter writer = new XmlTextWriter("data.xml",null);
    writer.Formatting = Formatting.Indented;
    doc.Save(writer);
  }
}

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

int main()
{
    // Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();
    doc->LoadXml(S"<item><name>wrench</name></item>");

    // Add a price element.
    XmlElement* newElem = doc->CreateElement(S"price");
    newElem->InnerText = S"10.95";
    doc->DocumentElement->AppendChild(newElem);

    // Save the document to a file and auto-indent the output.
    XmlTextWriter* writer = new XmlTextWriter(S"data.xml",0);
    writer->Formatting = Formatting::Indented;
    doc->Save(writer);
};

[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 名前空間 | XmlDocument.Save オーバーロードの一覧 | XmlTextWriter | XmlTextWriter.Formatting