Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Compact framework: how to read and write to a XML file

Compact framework: how to read and write to a XML file

Scheduled Pinned Locked Moved C#
tutorialxmlhelpquestionannouncement
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nuttynibbles
    wrote on last edited by
    #1

    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>
    </configuration

    note: 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);
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups