Reading from .config files (XPath problems with CF)

A

Andrea Grandi

Hello,

I've created my .config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="prova" value="pippo" />
</appSettings>
</configuration>

I tried reading this using the Configuration class in OpenNetCF with no
success. I tried to write to their forum, no reply.

I tried this XPath query "/configuration/appSettings/add[@key=" + key +
"]/@value" with no success :(

SelectNode and SelectSingleNode is not available in .Net CF.

I googled for two days, finding only samples that don't work (for
example this: http://www.dotnet247.com/247reference/msgs/58/290766.aspx)

I know that XMLDocument is loaded because in the debug I can see this:

? doc.OuterXml
"<?xml version="1.0" encoding="utf-8"?><configuration><appSettings><add
key="prova" value="pippo" /></appSettings></configuration>"

Can anyone help me please? I just want to read configuration from a
..config file in an easy way.

Thanks!
 
A

Alex Yakhnin

What happens when you are using OpenNETCF's Configuration class? Make sure
that the name of your config file is <exename>.exe.config
 
A

Andrea Grandi

Hello,
What happens when you are using OpenNETCF's Configuration class? Make sure
that the name of your config file is <exename>.exe.config

I've found another solution: I built a class that manually parse a
config.xml file and retrive the configuration.

Withe the OpenNetCF I had this situation:

I've this .config file:



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="prova" value="pippo" />
</appSettings>
</configuration>



The executable name is SlotMachines.exe and the config is
SlotMachines.exe.config
in the same folder of the .exe

I try to get the value with this cose:


Dim str As String
str = ConfigurationSettings.AppSettings("prova")



but the values is always nothing :(
 
S

Sergey Bogdanov

Include machine.config into your project and set build action for this
file to 'Content'. Then to make sure that machine.config is in the right
place you might write this code:

using System.IO;

....

const string ConfigDir = "\\windows\\config\\";
const string MachineConfig = "machine.config";
....

string AppDir =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);


string src = Path.Combine(AppDir, MachineConfig);
string dst = Path.Combine(ConfigDir, MachineConfig);
Directory.CreateDirectory(ConfigDir);

if (!File.Exists(dst)) File.Copy(src, dst, false);


Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top