欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月
extensibility
the three types of information in a standard ini file often did not suffice to store the information needed by the application. ive seen (and built) applications that rely on custom string formats to perform tasks beyond the ini file formats native capabilities. for example, its not uncommon to see ini files that contain strings that use separators to "squash" more values into an ini file:

   [testing]   emp1=bob johnson|accounting|04/03/2001 8:23:14|85   emp2=susan fielding|sales|03/23/2001 15:41:48|92

not 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>

its much easier to discover the meaning of the data in the xml version.

at a more complex level, although i havent 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—its 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.

wheres 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 theres 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 arent 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. nonetheless, .net configuration files have some advantages even over these custom external xml-formatted initialization files. in a future article, ill 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 devxs executive editor. hes 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 rjones@devx.com.
赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章