XmlDocument.Save 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 XML 文件儲存到指定位置。
多載
| 名稱 | Description |
|---|---|
| Save(Stream) |
將 XML 文件儲存到指定的串流中。 |
| Save(TextWriter) |
將 XML 文件儲存為指定的 TextWriter。 |
| Save(String) |
將 XML 文件儲存到指定的檔案中。 如果指定的檔案存在,此方法會覆寫它。 |
| Save(XmlWriter) |
將 XML 文件儲存為指定的 XmlWriter。 |
Save(Stream)
將 XML 文件儲存到指定的串流中。
public:
virtual void Save(System::IO::Stream ^ outStream);
public virtual void Save(System.IO.Stream outStream);
abstract member Save : System.IO.Stream -> unit
override this.Save : System.IO.Stream -> unit
Public Overridable Sub Save (outStream As Stream)
參數
- outStream
- Stream
你想存檔的串流。
例外狀況
此操作不會產生良好格式的 XML 文件(例如沒有文件元素或重複的 XML 宣告)。
備註
只有當 PreserveWhitespace 設為 true時,空白空間才會被保留。
目前 XmlDocument 物件的 XmlDeclaration 決定了已儲存文件中的編碼屬性。 編碼屬性的值取自屬性。XmlDeclaration.Encoding 如果 沒有 XmlDocument XmlDeclaration,或 XmlDeclaration 沒有編碼屬性,儲存的文件也不會有編碼屬性。
當文件被儲存時,會產生 xmlns 屬性,以正確持久化節點身份(本地名稱 + 命名空間 URI)。 例如,以下 C# 程式碼
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("item","urn:1"));
doc.Save(Console.Out);
產生此 XML 屬性 <item xmls="urn:1"/>。
此方法是 Microsoft 對文件物件模型(DOM)的擴充。
請注意,只有該 Save 方法會強制執行格式良好的 XML 文件。 其他 Save 所有過載僅保證一個良好形成的片段。
適用於
Save(TextWriter)
將 XML 文件儲存為指定的 TextWriter。
public:
virtual void Save(System::IO::TextWriter ^ writer);
public virtual void Save(System.IO.TextWriter writer);
abstract member Save : System.IO.TextWriter -> unit
override this.Save : System.IO.TextWriter -> unit
Public Overridable Sub Save (writer As TextWriter)
參數
- writer
- TextWriter
TextWriter你想要節省的金額。
例外狀況
此操作不會產生良好格式的 XML 文件(例如沒有文件元素或重複的 XML 宣告)。
備註
在 上的 TextWriter 編碼決定了所寫出的編碼(XmlDeclaration 節點的編碼被 的編碼取代 TextWriter)。 若 上未指定 TextWriter編碼,則 XmlDocument 存檔時未包含編碼屬性。
此方法是 Microsoft 對文件物件模型(DOM)的擴充。
請注意,只有該 Save 方法會強制執行格式良好的 XML 文件。 其他 Save 所有過載僅保證一個良好形成的片段。
適用於
Save(String)
將 XML 文件儲存到指定的檔案中。 如果指定的檔案存在,此方法會覆寫它。
public:
virtual void Save(System::String ^ filename);
public virtual void Save(string filename);
abstract member Save : string -> unit
override this.Save : string -> unit
Public Overridable Sub Save (filename As String)
參數
- filename
- String
你想儲存文件的檔案位置。
例外狀況
此操作不會產生良好格式的 XML 文件(例如沒有文件元素或重複的 XML 宣告)。
範例
以下範例將 XML 載入 XmlDocument 物件,修改後儲存為名為 data.xml的檔案。
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");
}
}
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
data.xml 檔案將包含以下 XML: <item><name>wrench</name><price>10.95</price></item>。
備註
只有當 PreserveWhitespace 將 設為 true時,輸出檔案中才會保留空白空間。
目前 XmlDocument 物件的 XmlDeclaration 決定了已儲存文件中的編碼屬性。 編碼屬性的值取自屬性。XmlDeclaration.Encoding 如果 沒有 XmlDocument XmlDeclaration,或 XmlDeclaration 沒有編碼屬性,儲存的文件也不會有編碼屬性。
當文件被儲存時,會產生 xmlns 屬性,以正確持久化節點身份(本地名稱 + 命名空間 URI)。 例如,以下 C# 程式碼
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("item","urn:1"));
doc.Save(Console.Out);
產生此 XML 屬性 <item xmls="urn:1"/>。
此方法是 Microsoft 對文件物件模型(DOM)的擴充。
請注意,只有該 Save 方法會強制執行格式良好的 XML 文件。 其他 Save 所有過載僅保證一個良好形成的片段。
適用於
Save(XmlWriter)
將 XML 文件儲存為指定的 XmlWriter。
public:
virtual void Save(System::Xml::XmlWriter ^ w);
public virtual void Save(System.Xml.XmlWriter w);
abstract member Save : System.Xml.XmlWriter -> unit
override this.Save : System.Xml.XmlWriter -> unit
Public Overridable Sub Save (w As XmlWriter)
參數
XmlWriter你想要節省的金額。
例外狀況
此操作不會產生良好格式的 XML 文件(例如沒有文件元素或重複的 XML 宣告)。
範例
以下範例將 XML 載入 XmlDocument 物件並儲存為檔案。
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);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
// Save the document to a file and auto-indent the output.
XmlWriter writer = XmlWriter.Create("data.xml", settings);
doc.Save(writer);
}
}
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)
Dim settings As New XmlWriterSettings()
settings.Indent = True
' Save the document to a file and auto-indent the output.
Dim writer As XmlWriter = XmlWriter.Create("data.xml", settings)
doc.Save(writer)
end sub
end class
備註
只有當 PreserveWhitespace 設為 true時,空白空間才會被保留。
在 上的 XmlWriter 編碼決定了所寫出的編碼(XmlDeclaration 節點的編碼被 的編碼取代 XmlWriter)。 若 上未指定 XmlWriter編碼,則 XmlDocument 存檔時未包含編碼屬性。
當文件被儲存時,會產生 xmlns 屬性以正確持久化節點身份(LocalName + NamespaceURI)。 例如,以下 C# 程式碼
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("item","urn:1"));
doc.Save(Console.Out);
產生此 XML 屬性:
<item
xmls="urn:1"/>
此方法是 Microsoft 對文件物件模型(DOM)的擴充。
請注意,只有該 Save 方法會強制執行格式良好的 XML 文件。 其他 Save 所有過載僅保證一個良好形成的片段。