Upgrade Your INI Files to XML with .NET (4)

发表于:2007-06-30来源:作者:点击数: 标签:
Extensibility The three types of information in a standard INI file often did not suffice to store the information needed by the application. I@#ve seen (and built) applications that rely on custom string formats to perform tasks beyond th

Extensibility
The three types of information in a standard INI file often did not suffice to store the information needed by the application. I@#ve seen (and built) applications that rely on custom string formats to perform tasks beyond the INI file format@#s native capabilities. For example, it@#s not uncommon to see INI files that contain strings that use separators to "squash" more values into an INI file:

   [testing]   emp1=Bob Johnson|Aclearcase/" target="_blank" >ccounting|04/03/2001 8:23:14|85   emp2=Susan Fielding|Sales|03/23/2001 15:41:48|92Not only are such files difficult to read and edit—and maintain—but they also require custom code to retrieve the item values and assign them to variables within the program. The data in such files is unusable by other programs without recreating the code to retrieve the data. In contrast, you can add new items to an XML-formatted INI file with few problems. At the most simplistic level, you can use the GetCustomIniAttribute and SetCustomIniAttribute methods to read, write, and delete custom strings, stored as attributes within the <item> elements. For example, the following XML listing shows the same data shown in the preceding INI file added as custom attributes.

   <?xml version="1.0" encoding="UTF-8"?>   <sections>     <comment>Company employees</comment>     <section name="employees">       <item key="employee1" value="Bob Johnson"           department="Accounting"           testedon="04/03/2001           8:23:14" score="85"/>       <item key="employee2"           value="Susan Fielding"           department="Sales"           testedon="03/23/2001 15:41:48"           score="92" />     </section>   </sections>It@#s much easier to discover the meaning of the data in the XML version.

At a more complex level, although I haven@#t implemented it in the sample code, you could add GetCustomIniElement and SetCustomIniElement methods to add custom child elements and values to the <item> elements. These methods would be overloaded to accept an element name and value, an XmlElement instance, or an XmlDocumentFragment instance, so you could make the file as complicated as necessary. The Extensibility button on the sample form contains code that shows how to use the extensibility methods.

Beyond the built-in extensibility methods, you can, of course, subclass the IniFileReader and override or add methods to do anything you like.

Dealing with Comments
You use the SetIniComments and GetIniComments methods to add and retrieve comments. The GetIniComments method returns a StringCollection containing the text of comments at either the file or section level. The SetIniComments method accepts a StringCollection containing a list of comments you want to add at either the file or section level. While this implementation is very crude, and could be greatly improved—for example, you could extend the class to attach comments directly to individual items—it@#s already an improvement over standard INI files, which provide no way to create or remove comments automatically. You can also add XML-formatted comments manually or use the DOM directly to add comments to an XML-formatted INI file.

Where@#s My INI File?
For straightforward (unextended) files, you can "get the original INI file back." In some cases, you will want to read and modify the INI file with a .NET application, but you still need access to the file in its original format usable by pre-.NET Windows applications. An XSLT transform performs the work of turning the file back into a standard INI file. You initiate the transform using the SaveAsIniFile method. However, extensibility comes at a price. If you make custom modifications to the file using the writeCustomIniAttribute method, those changes will not appear in the results of the transform. As there@#s little reason to translate the files back to standard INI format, that seems reasonable.

Having a separate file for storing application initialization information is a good idea. The .NET framework contains built-in methods for handling configuration data, but—as delivered—they aren@#t suitable for complex information structures. As you migrate existing applications into .NET, the INIFileReader class described in this article lets you use and even extend your existing INI files. N.netheless, .NET configuration files have some advantages even over these custom external XML-formatted initialization files. In a future article, I@#ll show you how to achieve the same level of functionality using a custom configuration handler, which will let you place configuration data directly into your machine.config or application configuration file.


A. Russell Jones, Ph.D.is DevX@#s Executive editor. He@#s a former reptile keeper and professional musician who now composes computer applications. His most recent books are Mastering ASP.NET with VB.NET and Mastering ASP.NET with Visual C# (both published by Sybex). Reach him by e-mail at .

原文转自:http://www.ltesting.net