Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. /* Class ConfUtils servant à créer des fichiers XML
  2.  * +readXML(String path) : ArrayList
  3.  * +writeXML(String path, String data)
  4.  * lang {EN, FR}
  5.  * theme {dark, light}
  6.  * experimental {true, false}
  7.  * mailNotification {true, false}
  8.  */
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Xml;
  13.  
  14. namespace ViewModel
  15. {
  16.     public class ConfUtils
  17.     {
  18.         private Dictionary<string, string> ReadXml(string path)
  19.         {
  20.             var parametersKey = new Dictionary<string, string>();
  21.             var confXml = new XmlDocument();
  22.             confXml.Load(path);
  23.  
  24.             if (confXml.DocumentElement == null) return parametersKey;
  25.             foreach (XmlNode node in confXml.DocumentElement.ChildNodes)
  26.             {
  27.                 parametersKey.Add(node.Name, node.InnerText);
  28.             }
  29.  
  30.             /* For debug purposes only, write in console what it reads
  31.             foreach (string key in parametersKey.Keys)
  32.             {
  33.                 Console.WriteLine("the parameter {0} has for value {1}.", key, parametersKey[key]);
  34.             }*/
  35.            
  36.             return parametersKey;
  37.         }
  38.        
  39.         private void WriteXml(string path, string element, string value)
  40.         {
  41.             XmlDocument confXml = new XmlDocument();
  42.             confXml.Load(path);
  43.             if (confXml.DocumentElement == null) return;
  44.             foreach (XmlNode node in confXml.DocumentElement.ChildNodes)
  45.             {
  46.                 if (node.Name == element)
  47.                 {
  48.                     node.InnerText = value;
  49.                 }
  50.             }
  51.             confXml.Save(path);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement