Creating XmlDeclarationsIf you are creating a new XMLDocument from scratch, you may want to start by creating an XmlDeclaration node to add to the node tree. The XmlDocument 's CreateXmlDeclaration method provides this functionality.
The CreateXmlDeclaration method takes three string parameters. The first parameter is the XML version number, which must always equal "1.0." If the string is any other value, an ArgumentException is thrown. The second parameter is the string value of the encoding attribute. The value must be a string supported by the System.Text.Encoding class; otherwise , you will not be able to save the document. You may pass in null or empty string if you do not want the encoding attribute to be written while saving the document. Also, if the XmlDocument is saving to either a TextWriter or an XmlTextWriter , the encoding value is discarded, and the encoding of the specified writer is used instead. The final parameter is the string value of the stand-alone attribute. This string must be either " yes " or " no ;" otherwise, an ArgumentException is raised. Setting the stand-alone attribute's value to null or String.Empty signifies that the stand-alone attribute should not be written when the document is saved. Listing 11.17C# // <?xml version="1.0" encoding="ASCII" standalone="yes"?> XmlDeclaration decl = XmlDeclaration("1.0", "ASCII", "yes"); // <?xml version="1.0" encoding="UTF8"?> decl = XmlDeclaration("1.0", "UTF8", null); // <?xml version="1.0"?> decl = XmlDeclaration("1.0", null, null); VB ' <?xml version="1.0" encoding="ASCII" standalone="yes"?> Dim decl As New XmlDeclaration("1.0", "ASCII", "yes") ' <?xml version="1.0" encoding="UTF8"?> decl = XmlDeclaration("1.0", "UTF8", Nothing) ' <?xml version="1.0"?> decl = XmlDeclaration("1.0", null, Nothing) |