Editare

Partajați prin


XmlDocument.Save Method

Definition

Saves the XML document to the specified location.

Overloads

Save(Stream)

Saves the XML document to the specified stream.

Save(TextWriter)

Saves the XML document to the specified TextWriter.

Save(String)

Saves the XML document to the specified file. If the specified file exists, this method overwrites it.

Save(XmlWriter)

Saves the XML document to the specified XmlWriter.

Save(Stream)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

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)

Parameters

outStream
Stream

The stream to which you want to save.

Exceptions

The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

Remarks

White space is preserved only if PreserveWhitespace is set to true.

The XmlDeclaration of the current XmlDocument object determines the encoding attribute in the saved document. The value of the encoding attribute is taken from the XmlDeclaration.Encoding property. 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.

When the document is saved, xmlns attributes are generated to persist the node identity (local name + namespace URI) correctly. For example, the following C# code

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

generates this xmls attribute <item xmls="urn:1"/>.

This method is a Microsoft extension to the Document Object Model (DOM).

Note that only the Save method enforces a well-formed XML document. All other Save overloads only guarantee a well-formed fragment.

Applies to

Save(TextWriter)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Saves 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)

Parameters

writer
TextWriter

The TextWriter to which you want to save.

Exceptions

The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

Remarks

The 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). If there was no encoding specified on the TextWriter, the XmlDocument is saved without an encoding attribute.

This method is a Microsoft extension to the Document Object Model (DOM).

Note that only the Save method enforces a well-formed XML document. All other Save overloads only guarantee a well-formed fragment.

Applies to

Save(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

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)

Parameters

filename
String

The location of the file where you want to save the document.

Exceptions

The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

Examples

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

The data.xml file will contain the following XML: <item><name>wrench</name><price>10.95</price></item>.

Remarks

White space is preserved in the output file only if PreserveWhitespace is set to true.

The XmlDeclaration of the current XmlDocument object determines the encoding attribute in the saved document. The value of the encoding attribute is taken from the XmlDeclaration.Encoding property. 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.

When the document is saved, xmlns attributes are generated to persist the node identity (local name + namespace URI) correctly. For example, the following C# code

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

generates this xmls attribute <item xmls="urn:1"/>.

This method is a Microsoft extension to the Document Object Model (DOM).

Note that only the Save method enforces a well-formed XML document. All other Save overloads only guarantee a well-formed fragment.

Applies to

Save(XmlWriter)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Saves 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)

Parameters

w
XmlWriter

The XmlWriter to which you want to save.

Exceptions

The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

Examples

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

Remarks

White space is preserved only if PreserveWhitespace is set to true.

The 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). If there was no encoding specified on the XmlWriter, the XmlDocument is saved without an encoding attribute.

When the document is saved, xmlns attributes are generated to persist the node identity (LocalName + NamespaceURI) correctly. For example, the following C# code

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

generates this xmls attribute:

<item
    xmls="urn:1"/>

This method is a Microsoft extension to the Document Object Model (DOM).

Note that only the Save method enforces a well-formed XML document. All other Save overloads only guarantee a well-formed fragment.

See also

Applies to