XmlDocument.Save 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 XML 文档保存到指定的位置。
重载
| 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 -> unitPublic Overridable Sub Save (outStream As Stream)参数
- outStream
- Stream
要保存到其中的流。
例外
该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。
注解
仅当设置为 true 时PreserveWhitespace,才会保留空格。
当前 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);  
生成此 xmls 属性 <item xmls="urn:1"/>。
此方法是文档对象模型 (DOM) 的 Microsoft 扩展。
请注意,只有 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 -> unitPublic Overridable Sub Save (writer As TextWriter)参数
- writer
- TextWriter
要保存到其中的 TextWriter。
例外
该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。
注解
编码 TextWriter 确定写出 (XmlDeclaration 节点的编码将替换为) 的 TextWriter 编码。 如果没有指定 TextWriter编码,则 XmlDocument 不保存编码属性。
此方法是文档对象模型 (DOM) 的 Microsoft 扩展。
请注意,只有 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 -> unitPublic Overridable Sub Save (filename As String)参数
- filename
- String
要将文档保存到其中的文件的位置。
例外
该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。
示例
以下示例将 XML 加载到 XmlDocument 对象中,对其进行修改,然后将其保存到名为data.xml的文件。
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew 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" );
}
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>
注解
仅当设置为 true 时PreserveWhitespace,才会在输出文件中保留空格。
当前 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);  
生成此 xmls 属性 <item xmls="urn:1"/>。
此方法是文档对象模型 (DOM) 的 Microsoft 扩展。
请注意,只有 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 -> unitPublic Overridable Sub Save (w As XmlWriter)参数
要保存到其中的 XmlWriter。
例外
该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。
示例
以下示例将 XML 加载到对象 XmlDocument 中,并将其保存到文件中。
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew 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 = gcnew XmlTextWriter( "data.xml", nullptr );
   writer->Formatting = Formatting::Indented;
   doc->Save( writer );
}
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
注解
仅当设置为 true 时PreserveWhitespace,才会保留空格。
编码 XmlWriter 确定写出 (XmlDeclaration 节点的编码将替换为) 的 XmlWriter 编码。 如果没有指定 XmlWriter编码,则 XmlDocument 不保存编码属性。
保存文档后,将生成 xmlns 属性以正确保存节点标识 (LocalName + NamespaceURI) 。 例如,以下 C# 代码
XmlDocument doc = new XmlDocument();  
doc.AppendChild(doc.CreateElement("item","urn:1"));  
doc.Save(Console.Out);  
生成此 xmls 属性:
<item  
    xmls="urn:1"/>  
此方法是文档对象模型 (DOM) 的 Microsoft 扩展。
请注意,只有 Save 该方法强制实施格式良好的 XML 文档。 所有其他 Save 重载仅保证格式良好的片段。