欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

upgrade your ini files to xml with .net (cont.)

the xml ini file structure
the inifilereader project sample code that accompanies this article reads standard ini files or xml-formatted ini files (see table 1 for a complete list of methods and properties). the inifilereader class constructor requires a file name. the class first tries to open the specified file as an xml file, using the xmldocument.load method. if that fails, the class assumes the file is in the ini format. it then creates a very simple default xml string and loads that, using the xmldocument.loadxml method, after which it opens the file in text mode and parses the lines of the file adding elements for the sections, items, and comments in the order they appear (see listing 1). as an example, the sample ini file shown earlier looks like this after the inifilereader finishes loading it.

   <?xml version="1.0" encoding="utf-8"?>
   <sections>
     <comment> company employees</comment>
     <section name="employee1">
       <item key="name" value="bob johnson" />
       <item key="department" value="accounting" />
     </section>
     <section name="employee2">
       <item key="name" value="susan fielding" />
       <item key="department" value="sales" />
     </section>
   </sections>

getting the ini file contents into this simple xml structure makes it easy to mimic and extend the actions that you can perform with a standard ini file—and makes them easier to remember. the root element can contain any number of child <comment> or <section> elements, each of which can contain any number of <item> or <comment> elements.

one question you may have: why doesnt the project use the standard api functions through dllimport as discussed in use com interop to read and write to ini files with .net? the answer is that the standard api functions provide no way to read comments, so you cant get a complete translation using the api functions alone. instead, for this particular purpose, its better to parse the file line by line.

retrieving values
to retrieve the value of an item, you use the getinivalue method, which accepts sectionname and keyname parameters. the method creates an xpath query that searches for the section with a name attribute matching the supplied section name, and then searches within that section for an item with a key attribute matching the supplied key name. if the xpath query matches an item, the function returns the text value of the value attribute of that item; otherwise it returns null.

   public string getinivalue(string sectionname,
      string keyname) {
      xmlnode n = null;
      if ((sectionname != null) && (sectionname != "")
         && (keyname != null) && (keyname != "")) {
         n = getitem(sectionname, keyname);
         if (n != null) {
            return(n.attributes.getnameditem
               ("value").value);
         }
      }
      return null;
   }

there is one major difference between xml-formatted files and ini files—xml files are case sensitive, while standard ini files arent. the inifilereader class deals with this by treating all data as case-insensitive by default. see the sidebar "dealing with xmls case sensitivity for a more detailed explanation.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)

相关推荐

  • 暂无文章