save Method (DOMDocument)
Collapse the table of content
Expand the table of content

save Method (DOMDocument)


Saves an XML document to the specified location.;


An object. The object can represent a file name, an ASP Response object, a DOMDocument object, or a custom object that supports persistence. See Remarks for more information.

If your DOMDocument contains unformatted XML, you might want to format it before saving. There is no flag in DOMDocument that can be set to indent your XML. In this situation, you might want to use the SAX writer to produce the resulting XML.

    VARIANT destination);


The type of object to save. This object can represent a file name, an ASP Response object, an XML document object, or a custom object that supports persistence. See Remarks for more information.


The value returned if successful.


The value returned if the document contains a character that does not belong in the specified encoding. The character must use a numeric entity reference. For example, the Japanese Unicode character 20013 does not fit into the encoding Windows-1250 (the Central European alphabet) and therefore must be represented in markup as the numeric entity reference 中 or 中 This version of save does not automatically convert characters to the numeric entity references.


The value returned if a string was provided, but it is not a valid file name.


The value returned if a save operation is not permitted.


The value returned if the save operation must allocate buffers.

(Other values)

Any other file system error can be returned in the save(string) case.

BOOL DOMDocSaveLocation()
   BOOL bResult = FALSE;
   IXMLDOMDocument *pIXMLDOMDocument = NULL;
   HRESULT hr;

      _variant_t varString = _T("D:\\sample.xml");
      // Initialize pIXMLDOMDocument (create a DOMDocument).
      // Load document.
      hr = pIXMLDOMDocument->save(varString);
         bResult = TRUE;
   // Release the IXMLDOMDocument interface.
   // Release the IXMLDOMDocument interface when finished with it.
   return bResult;

The behavior differs based on the object specified by the objTarget parameter.




Specifies the file name. This must be a file name rather than a URL. The file is created, if necessary, and the contents are replaced entirely with the contents of the saved document. This mode is not intended for use from a secure client, such as Microsoft Internet Explorer.

dim xmldoc
    set xmldoc = Server.CreateObject("Msxml2.DOMDocument.3.0")

The ASP Response object sends the document back to the client that invoked the ASP script.

Snippet section ef1f2f5fc48b45c09746df1f7c044c08 of code snippet {"project_id":"79a1f4df-7306-4995-a144-4d201f89c75c","entity_id":"879122c3-fa1d-4776-95f4-2852ba82c8c7","entity_type":"CodeSnippet","locale":"en-US"} in source file ({"filename":"/vbs/ef1f2f5fc48b45c09746df1f7c044c08.vbs","blob_type":"Source","blob_id":"-002fvbs-002fef1f2f5fc48b45c09746df1f7c044c08-002evbs","blob_revision":1}), line 1 is missing the closing tag.

               IXMLDocument Object

Duplicates the original document. It is the equivalent of saving the document and reparsing it. The document goes through full persistence through XML markup, thereby testing the persistability of your XML document.

Snippet section f508fc5f51eb4c27880af22596e3ba1a of code snippet {"project_id":"79a1f4df-7306-4995-a144-4d201f89c75c","entity_id":"879122c3-fa1d-4776-95f4-2852ba82c8c7","entity_type":"CodeSnippet","locale":"en-US"} in source file ({"filename":"/html/f508fc5f51eb4c27880af22596e3ba1a.html","blob_type":"Source","blob_id":"-002fhtml-002ff508fc5f51eb4c27880af22596e3ba1a-002ehtml","blob_revision":1}), line 1 is missing the closing tag.

Custom object supporting persistence

Any other custom COM object that supports QueryInterface for IStream, IPersistStream, or IPersistStreamInit can also be provided here, and the document will be saved accordingly. In the IStream case, the IStream Write method will be called as it saves the document; in the IPersistStream case, IPersistStreamLoad will be called with an IStream that supports the Read, Seek, and Stat methods.

External entity references in <DOCTYPE>, <ENTITY>, <NOTATION>, and XML namespace declarations are not changed; they point to the original document. A saved XML document might not load if the URLs are not accessible from the location in which you saved the document.

Character encoding is based on the encoding attribute in the XML declaration, such as <?xml version="1.0" encoding="windows-1252"?>. When no encoding attribute is specified, the default setting is UTF-8.

Validation is not performed during save, which can result in an invalid document that does not load again because of a specified document type definition (DTD).

This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).

         Implemented in: MSXML 3.0 and MSXML 6.0

© 2015 Microsoft