1 string configFileName = Application.StartupPath + @"\app.config";
2 XmlDocument configFile = new XmlDocument();
3 configFile.Load(configFileName);
4 XmlNode configurations = configFile.SelectSingleNode("descendant::configuration");
5 XmlNode appConfig = configFile.SelectSingleNode("descendant::appSettings");
6 if (appConfig == null)
7 {
8 appConfig = configFile.CreateElement("appSettings");
9 configurations.AppendChild(appConfig);
10 }
11 // il nodo deve avere la forma: <add key= “Nome del DB”
12 // value=”Stringa di connessione” />
13 XmlNode node = configFile.CreateElement("add");
14 XmlAttribute key = configFile.CreateAttribute("key");
15 key.Value = "DBUTENTI";
16 // aggiungo l’attributo al nodo
17 node.Attributes.SetNamedItem(key);
18 XmlAttribute val = configFile.CreateAttribute("value");
19 val.Value = "Data Source=SRVSQLCAAF;Initial Catalog=ANAUTENTI;Integrated Security=True";
20 node.Attributes.SetNamedItem(val);
21 // aggiungo il nodo all’elemento superiore
22 appConfig.AppendChild(node);
23 node = configFile.CreateElement("add");
24 key = configFile.CreateAttribute("key");
25 key.Value = "DBCOMUNI";
26 node.Attributes.SetNamedItem(key);
27 val = configFile.CreateAttribute("value");
28 val.Value = "Data Source=SRVSQLCAAF;Initial Catalog=COMUNI;Integrated Security=True";
29 node.Attributes.SetNamedItem(val);
30 appConfig.AppendChild(node);
31 configFile.Save(configFileName);