Re: Best thing to simulate an Ini file

From: Alexander Muylaert (Remove_amg_at_pandora.be)
Date: 11/14/04

  • Next message: Alex Feinman [MVP]: "Re: autorun"
    Date: Sun, 14 Nov 2004 08:48:50 +0100
    
    

    Thanks

    "Darren Shaffer" <darrenshaffer@discussions.microsoft.com> wrote in message
    news:uEjLbnhyEHA.1524@TK2MSFTNGP09.phx.gbl...
    > Use a simple class to read and write to a configuration file on either the
    > device or the desktop. Here's a sample of the xml file and a class to
    > read/write
    > named/value pairs.
    >
    > -Darren Shaffer
    >
    > Note that if you have many fields to store, you may want to refactor this
    > code to
    > use XmlTextReader and XmlTextWriter which are more efficient than
    > XmlDocument on larger sets. The code below assumes your xml file is named
    > "config.xml".
    >
    > ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    > <?xml version="1.0" encoding="utf-8" ?>
    > <configuation>
    > <appSettings key="MapPointDataSource" value="North America"/>
    > <appSettings key="MapPointCountry" value="United States"/>
    > <appSettings key="MapPointLanguage" value="en"/>
    > <appSettings key="MapStyle" value="Road"/>
    > <appSettings key="MapFont" value="Smaller"/>
    > <appSettings key="UnitOfMeasure" value="Metric - Kilometers"/>
    > <appSettings key="GPSPrecision" value="5"/>
    > <appSettings key="GPSSampleFreq" value="5"/>
    > <appSettings key="RefreshMap" value="true"/>
    > <appSettings key="LastLat" value="39.76547"/>
    > <appSettings key="LastLong" value="-104.97158"/>
    > <appSettings key="URL" value="http://staging.mappoint.net"/>
    > </configuation>
    > ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    > using System;
    > using System.IO;
    > using System.Reflection;
    > using System.Text;
    > using System.Xml;
    >
    > namespace Utilities
    > {
    > class Configuration
    > {
    > private static string _appPath =
    > Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
    > private static string _myConfigFile = _appPath + "\\config.xml";
    >
    > public Configuration()
    > {
    >
    > }
    > public static string GetAppSetting(string strKey)
    > {
    > string sValue = "";
    > XmlDocument configDocument = new XmlDocument();
    > configDocument.Load(_myConfigFile);
    >
    > foreach (XmlNode node in configDocument.ChildNodes)
    > {
    > foreach (XmlNode childnode in node.ChildNodes)
    > {
    > if
    > (childnode.Attributes["key"].Value.ToLower().Equals(strKey.ToLower()))
    > {
    > sValue = childnode.Attributes["value"].Value;
    > break;
    > }
    > }
    > if (sValue.Length > 0)
    > {
    > break;
    > }
    > }
    > return sValue;
    > }
    >
    > public static bool SetAppSetting(string sKey, string sValue)
    > {
    > if ( sKey == null || sValue == null )
    > return false;
    >
    > XmlDocument configDocument = new XmlDocument();
    > configDocument.Load(_myConfigFile);
    >
    > foreach (XmlNode node in configDocument.ChildNodes)
    > {
    > foreach (XmlNode childnode in node.ChildNodes)
    > {
    > if
    > (childnode.Attributes["key"].Value.ToLower().Equals(sKey.ToLower()))
    > {
    > childnode.Attributes["value"].Value = sValue;
    > configDocument.Save(_myConfigFile);
    > return true;
    > }
    > }
    > }
    > return false;
    > }
    >
    > }
    > }
    >
    >
    >
    > "Alexander Muylaert" <Remove_amg@pandora.be> wrote in message
    > news:%23$k9aPWyEHA.2568@TK2MSFTNGP11.phx.gbl...
    >> Hi
    >>
    >> What would be the best way to have something like an ini file that works
    >> both on the full and on the compact1 framework?
    >>
    >> I need to save 1 - x different configuration files.
    >>
    >> kind regards en many thanks
    >>
    >> Alexander
    >>
    >
    >


  • Next message: Alex Feinman [MVP]: "Re: autorun"

    Relevant Pages

    • Re: Saving configuration to file in C#?
      ... > class I add to my projects and a sample XML file. ... > foreach (XmlNode node in configDocument.ChildNodes) ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: Saving configuration to file in C#?
      ... A simple xml file can be used on device for this purpose and also as a place ... foreach (XmlNode node in configDocument.ChildNodes) ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: Best thing to simulate an Ini file
      ... Use a simple class to read and write to a configuration file on either the ... Here's a sample of the xml file and a class to ... foreach (XmlNode node in configDocument.ChildNodes) ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: csproj/vbproj definition available?
      ... and A Project represents an MSBuild ... save to an XML file, preserving most whitespace and all XML comments." ... foreach ... foreach (BuildItemGroup itemGroup in project.ItemGroups) ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: XML::LibXML navigation
      ... I have to do some sanity checks on a large xml file of addresses. ... I can locate them easily enough but I am struggling to navigate back up the DOM to access the code so I can record the code with faulty addresses. ... foreach my $l { ...
      (perl.beginners)

    Loading