Exemplo n.º 1
0
        public static SettingFile GetSettings(string fileName)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fileName);

                var settingFile = new SettingFile();

                var root = doc.SelectSingleNode("//root");

                foreach (XmlNode node in root.ChildNodes)
                {
                    Dictionary <string, object> dic = new Dictionary <string, object>();

                    foreach (XmlAttribute attr in node.Attributes)
                    {
                        dic.Add(attr.Name, attr.Value);
                    }
                    settingFile.Items.Add(dic);
                }

                return(settingFile);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public static void SaveFile(string filename, SettingFile settings)
        {
            try
            {
                XmlDocument doc = new XmlDocument();

                var declara = doc.CreateXmlDeclaration("1.0", "utf-8", null);

                doc.AppendChild(declara);

                XmlElement root = doc.CreateElement("root");


                foreach (var item in settings.Items)
                {
                    XmlElement el = doc.CreateElement("item");

                    foreach (var attr in item)
                    {
                        el.SetAttribute(attr.Key.ToString(), attr.Value.ToString());
                    }
                    root.AppendChild(el);
                }

                doc.AppendChild(root);
                doc.Save(filename);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }