Creating XmlDeclarations


Creating XmlDeclarations

If 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.

PERFORMANCE OF XmlDocument SEARCHING

Given that the GetElementsByTagName may search elements at any depth, it is important to design your XML document and search methods carefully . Your XML should be designed so that XML elements with similar names are found at similar depths. Also, the GetElementsByTagName should be called on an XmlElement to prevent searching the whole document. Finally, try to narrow the search area as much as possible by finding the ancestor XmlElement closest to the target elements.


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.17
 C# // <?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) 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net