Read/Write to xml that was generated using csd

By : Fyber

I've used the Configuration Section Designer (csd) to generate an xml configuration file to my application.

Now, I want to work with this xml file (on c#) and do 2 things:

1. read a specific value (searching by its field), something like

txtbox_username.Text = ConfigurationManager.AppSettings["userName"];

2. write a specific value, something like

config.AppSettings.Settings["userName"].Value = txtbox_username.Text;


  • p.s: this is how i perform read/write for regular xml files that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
add key="userName" value="Value1"

but the csd generated xml looks different...

<?xml version="1.0"?>
    <section name="sectionName" type="Sample.ConfigurationSection.sectionName, Sample.ConfigurationSection, Version=, Culture=neutral, PublicKeyToken=null"/>
  <sectionName xmlns="Sample.ConfigurationSection">
    <logFile debugLevel="3"  filePath="c:\new" maxFileCount="10" maxFileLength="1000"/>
    <results path="C:\Results"/>
    <details user="blabla" pass="12345" code="abc"/>
    <stuff fromAddress="[email protected]" toAddress="[email protected]" sendMail="true""/>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

here, I want to edit and save, for example, user/pass/code fields.

By : Fyber


In short (and using these extensions: you can do this to read/write the user,

XElement file = XElement.Load(xmlFile);
XNamespace ns = "Sample.ConfigurationSection";
XElement section = file.GetElement(ns   "sectionName");
string user = section.Get("details/user", string.Empty);
section.Set("details/user", "bubba", true); // true = set as attribute

This video can help you solving your question :)
By: admin