Saving and Writing a Document

When you load and save an XmlDocument, the saved document may differ from the original in the following ways:

  • If the PreserveWhitespace property is set to true before the Save method is called, white space in the document is preserved in the output; if this property is false, XmlDocument auto-indents the output.

  • All the white space between attributes is reduced to a single space character.

  • The white space between elements is changed. Significant white space is preserved and insignificant white space is not. But when the document is saved, it will use the XmlTextWriter Indenting mode by default to neatly print the output to make it more readable.

  • The quote character used around attribute values is changed to double quote by default. You can use the QuoteChar property on XmlTextWriter to set the quote character to either double quote or single quote.

  • By default, numeric character entities like { are expanded.

  • The byte-order mark found in the input document is not preserved. UCS-2 is saved as UTF-8 unless you explicitly create an XML declaration that specifies a different encoding.

  • If you want to write out the XmlDocument into a file or stream, the output written out is the same as the content of the document. That is, the XmlDeclaration is written out only if there is one contained in the document, and the encoding used when writing out the document is the same encoding given in the declaration node.

Writing an XmlDeclaration

The XmlDocument and XmlDeclaration members of OuterXml, InnerXml, and WriteTo, in addition to the XmlDocument methods of Save and WriteContentTo, create an XML declaration.

For the XmlDocument properties of OuterXml, InnerXml, and the Save, WriteTo, and WriteContentTo methods, the encoding written out in the XML declaration is taken from the XmlDeclaration node. If there is no XmlDeclaration node, XmlDeclaration is not written out. If there is no encoding in the XmlDeclaration node, encoding is not written out in the XML declaration.

The XmlDocument.Save and XmlDocument.Save methods always write out an XmlDeclaration. These methods take the encoding from the writer that it is writing to. That is, the encoding value on the writer overrides the encoding on the document and in the XmlDeclaration. For example, the following code does not write an encoding in the XML declaration found in the output file out.xml.

Dim doc As New XmlDocument()  
Dim tw As XmlTextWriter = New XmlTextWriter("out.xml", Nothing)  
doc.Load("text.xml")  
doc.Save(tw)  
XmlDocument doc = new XmlDocument();  
XmlTextWriter tw = new XmlTextWriter("out.xml", null);  
doc.Load("text.xml");  
doc.Save(tw);  

For the Save method, the XML declaration is written out using the WriteStartDocument method in the XmlWriter class. Therefore, overwriting the WriteStartDocument method changes how the start of the document is written.

For the XmlDeclaration members of OuterXml, WriteTo, and InnerXml, if the Encoding property is not set, no encoding is written out. Otherwise, the encoding written out in the XML declaration is the same as the encoding found in the Encoding property.

Writing Document Content Using the OuterXml Property

The OuterXml property is a Microsoft extension to the World Wide Web Consortium (W3C) XML Document Object Model (DOM) standards. The OuterXml property is used to get the markup of the whole XML document, or just the markup of a single node and its child nodes. OuterXml returns the markup representing the given node and all its child nodes.

The following code sample shows how to save a document in its entirety as a string.

Dim mydoc As New XmlDocument()  
' Perform application needs here, like mydoc.Load("myfile");  
' Now save the entire document to a string variable called "xml".  
Dim xml As String = mydoc.OuterXml  
XmlDocument mydoc = new XmlDocument();  
// Perform application needs here, like mydoc.Load("myfile");  
// Now save the entire document to a string variable called "xml".  
string xml = mydoc.OuterXml;  

The following code sample shows how to save only the document element.

' For the content of the Document Element only.  
Dim xml As String = mydoc.DocumentElement.OuterXml  
// For the content of the Document Element only.  
string xml = mydoc.DocumentElement.OuterXml;  

In contrast, you can use the InnerText property if you want the content of child nodes.

See also