指定したファイルに XML ドキュメントを保存します。
Overloads Public Overridable Sub Save( _
ByVal filename As String _)
[C#]
public virtual void Save(stringfilename);
[C++]
public: virtual void Save(String* filename);
[JScript]
public function Save(
filename : String);
パラメータ
- filename
ドキュメントを保存するファイルの位置。
例外
例外の種類 | 条件 |
---|---|
XmlException | 操作の結果、整形式の XML ドキュメントにならない場合があります (ドキュメント要素がない、XML 宣言の重複など)。 |
解説
空白は PreserveWhitespace が true に設定されている場合だけ、出力ファイルに保存されます。
保存されたドキュメントのエンコーディング属性は、現在の XmlDocument オブジェクトの XmlDeclaration によって決定されます。エンコーディング属性の値は、 XmlDeclaration.Encoding プロパティから取得されます。 XmlDocument に XmlDeclaration がない場合、または XmlDeclaration にエンコーディング属性がない場合、保存されたドキュメントにエンコーディング属性は設定されません。
ドキュメントが保存されるときに、xmlns 属性が生成され、ノード ID (ローカル名 + 名前空間 URI) を正確に永続化します。たとえば、次に示す 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. White space is
' preserved (no white space).
doc.PreserveWhitespace = true
doc.Save("data.xml")
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. White space is
// preserved (no white space).
doc.PreserveWhitespace = true;
doc.Save("data.xml");
}
}
[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. White space is
// preserved (no white space).
doc->PreserveWhitespace = true;
doc->Save(S"data.xml");
}
[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 オーバーロードの一覧