Compact framework: how to read and write to a XML file
-
Hi, i got a AppSettings.exe.xml to read and write to. it looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="dir" value="\directory"/>
<add key="fieldname1" value="\value1"/>
<add key="fieldname2" value="\value2"/>
</appSettings>
</configuration>reading frm xml is not of a problem. but writing is.. everytime i write to the xml, the last char will get trim off. for example if i wanna write or append, the xml will look like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="dir" value="\directory"/>
<add key="fieldname1" value="\value1"/>
<add key="fieldname2" value="\value2"/>
</appSettings>
</configurationnote: the missing '>'. And if writing continue, more chars will get trim off im using this class to do the read and write to xml: namespace used:
using System;
using System.Xml;
using System.Configuration;
using System.Reflection;using OpenNETCF.Configuration;
using System.Windows.Forms;public class ConfigSettings
{
private ConfigSettings() { }public static string ReadSetting(string key) { return ConfigurationSettings.AppSettings\[key\]; } public static void WriteSetting(string key, string value) { // load config document for current assembly XmlDocument doc = loadConfigDocument(); // retrieve appSettings node XmlNode node = doc.SelectSingleNode("//appSettings"); if (node == null) throw new InvalidOperationException("appSettings section not found in config file."); try { // select the 'add' element that contains the key XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add\[@key='{0}'\]", key)); if (elem != null) { // add value for key elem.SetAttribute("value", value); } else { // key was not found so create the 'add' element // and set it's key/value attributes elem = doc.CreateElement("add"); elem.SetAttribute("key", key); elem.SetAttribute("value", value);