Läs på engelska Redigera

Dela via


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.

C#
public virtual void Save(System.IO.Stream outStream);

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

C#
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

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Save(TextWriter)

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

Saves the XML document to the specified TextWriter.

C#
public virtual void Save(System.IO.TextWriter writer);

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

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
public virtual void Save(string filename);

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.

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");
  }
}

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

C#
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

.NET 10 och andra versioner
Produkt Versioner
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Save(XmlWriter)

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

Saves the XML document to the specified XmlWriter.

C#
public virtual void Save(System.Xml.XmlWriter w);

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.

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

   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);
  }
}

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

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

generates this xmls attribute:

XML
<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

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0