XmlDocument.Save 方法

定义

将 XML 文档保存到指定的位置。Saves the XML document to the specified location.

重载

Save(Stream)

将 XML 文档保存到指定的流。Saves the XML document to the specified stream.

Save(TextWriter)

将 XML 文档保存到指定的 TextWriterSaves the XML document to the specified TextWriter.

Save(String)

将 XML 文档保存到指定的文件。Saves the XML document to the specified file. 如果存在指定文件,则此方法会覆盖它。If the specified file exists, this method overwrites it.

Save(XmlWriter)

将 XML 文档保存到指定的 XmlWriterSaves the XML document to the specified XmlWriter.

Save(Stream)

将 XML 文档保存到指定的流。Saves the XML document to the specified stream.

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

要保存到其中的流。The stream to which you want to save.

例外

该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

注解

仅当设置为时,才保留空白 PreserveWhitespace trueWhite space is preserved only if PreserveWhitespace is set to true.

当前对象的 XmlDeclaration XmlDocument 确定已保存文档中的编码属性。The XmlDeclaration of the current XmlDocument object determines the encoding attribute in the saved document. 编码特性的值是从属性获取的 XmlDeclaration.EncodingThe value of the encoding attribute is taken from the XmlDeclaration.Encoding property. 如果没有 XmlDocument XmlDeclaration,或者 XmlDeclaration 没有编码属性,则保存的文档不会有任何一个。If the XmlDocument does not have an XmlDeclaration, or if the XmlDeclaration does not have an encoding attribute, the saved document will not have one either.

保存文档时,会生成 xmlns 特性来持久保存节点标识 (本地名称 + 命名空间 URI) 正确保存。When the document is saved, xmlns attributes are generated to persist the node identity (local name + namespace URI) correctly. 例如,下面的 c # 代码For example, the following C# code

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

生成此 xml 属性 <item xmls="urn:1"/>generates this xmls attribute <item xmls="urn:1"/>.

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

请注意,只有 Save 方法强制执行格式正确的 XML 文档。Note that only the Save method enforces a well-formed XML document. 所有其他 Save 重载只保证格式正确的片段。All other Save overloads only guarantee a well-formed fragment.

适用于

Save(TextWriter)

将 XML 文档保存到指定的 TextWriterSaves the XML document to the specified 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

要保存到其中的 TextWriterThe TextWriter to which you want to save.

例外

该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

注解

上的编码 TextWriter 决定了写出的编码 (将 XmlDeclaration 节点的编码替换为) 的编码 TextWriterThe encoding on the TextWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is replaced by the encoding of the TextWriter). 如果未在上指定编码,则 TextWriter XmlDocument 会保存但不使用编码属性。If there was no encoding specified on the TextWriter, the XmlDocument is saved without an encoding attribute.

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

请注意,只有 Save 方法强制执行格式正确的 XML 文档。Note that only the Save method enforces a well-formed XML document. 所有其他 Save 重载只保证格式正确的片段。All other Save overloads only guarantee a well-formed fragment.

适用于

Save(String)

将 XML 文档保存到指定的文件。Saves the XML document to the specified file. 如果存在指定文件,则此方法会覆盖它。If the specified file exists, this method overwrites it.

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

要将文档保存到其中的文件的位置。The location of the file where you want to save the document.

例外

该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

示例

下面的示例将 XML 加载到一个 XML 对象中,对其进行修改,然后将其保存到名为 data.xml 的文件中。The following example loads XML into an XmlDocument object, modifies it, and then saves it to a file named 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>The data.xml file will contain the following XML: <item><name>wrench</name><price>10.95</price></item>.

注解

只有在设置为时,才会在输出文件中保留空白 PreserveWhitespace trueWhite space is preserved in the output file only if PreserveWhitespace is set to true.

当前对象的 XmlDeclaration XmlDocument 确定已保存文档中的编码属性。The XmlDeclaration of the current XmlDocument object determines the encoding attribute in the saved document. 编码特性的值是从属性获取的 XmlDeclaration.EncodingThe value of the encoding attribute is taken from the XmlDeclaration.Encoding property. 如果没有 XmlDocument XmlDeclaration,或者 XmlDeclaration 没有编码属性,则保存的文档不会有任何一个。If the XmlDocument does not have an XmlDeclaration, or if the XmlDeclaration does not have an encoding attribute, the saved document will not have one either.

保存文档时,会生成 xmlns 特性来持久保存节点标识 (本地名称 + 命名空间 URI) 正确保存。When the document is saved, xmlns attributes are generated to persist the node identity (local name + namespace URI) correctly. 例如,下面的 c # 代码For example, the following C# code

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

生成此 xml 属性 <item xmls="urn:1"/>generates this xmls attribute <item xmls="urn:1"/>.

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

请注意,只有 Save 方法强制执行格式正确的 XML 文档。Note that only the Save method enforces a well-formed XML document. 所有其他 Save 重载只保证格式正确的片段。All other Save overloads only guarantee a well-formed fragment.

适用于

Save(XmlWriter)

将 XML 文档保存到指定的 XmlWriterSaves the XML document to the specified 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)

参数

w
XmlWriter

要保存到其中的 XmlWriterThe XmlWriter to which you want to save.

例外

该操作不会生成格式标准的 XML 文档(例如,没有文档元素或 XML 声明重复)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

示例

下面的示例将 XML 加载到 XmlDocument 对象中,并将其保存到文件中。The following example loads XML into an XmlDocument object and saves it out to a file.

#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

注解

仅当设置为时,才保留空白 PreserveWhitespace trueWhite space is preserved only if PreserveWhitespace is set to true.

上的编码 XmlWriter 决定了写出的编码 (将 XmlDeclaration 节点的编码替换为) 的编码 XmlWriterThe encoding on the XmlWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is replaced by the encoding of the XmlWriter). 如果未在上指定编码,则 XmlWriter XmlDocument 会保存但不使用编码属性。If there was no encoding specified on the XmlWriter, the XmlDocument is saved without an encoding attribute.

保存文档时,会生成 xmlns 特性,以将节点标识 (LocalName + NamespaceURI 正确保存) 。When the document is saved, xmlns attributes are generated to persist the node identity (LocalName + NamespaceURI) correctly. 例如,下面的 c # 代码For example, the following C# code

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

生成此 xml 属性:generates this xmls attribute:

<item  
    xmls="urn:1"/>  

此方法是文档对象模型 (DOM) 的 Microsoft 扩展。This method is a Microsoft extension to the Document Object Model (DOM).

请注意,只有 Save 方法强制执行格式正确的 XML 文档。Note that only the Save method enforces a well-formed XML document. 所有其他 Save 重载只保证格式正确的片段。All other Save overloads only guarantee a well-formed fragment.

另请参阅

适用于