Results 1 to 10 of 14
Threaded View
-
16th Jun 2010, 10:25 PM #1OPლ(ಠ益ಠლ)Website's:
extremecoderz.com[c#] XML Reader / Writer
Here's a handly little class that i use very often to read/write XML files. Its a very handy little tool to save settings or anything else you wish to read/write often.
Hope you find it useful
Useage:
Code:setting mySettings = new settings("myFile.xml"); // To Retrieve Values string myString = mySettings.getSettings("myNodeName"); // To Set Values mySetting.setSettings("myNodeName", "myValue");
Code:using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; namespace Jayfella.XMLParser { class Settings { private string xmlFile; private XmlDocument xmldoc = new XmlDocument(); public Settings(string xmlFile) { this.xmlFile = xmlFile; initXml(); } private bool initXml() { try { xmldoc.Load(xmlFile); } catch (Exception) { TextWriter writer = new StreamWriter(xmlFile); writer.WriteLine("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); writer.WriteLine("<Settings>"); writer.WriteLine("</Settings>"); writer.Close(); } return true; } public string getSetting(string settingNode) { xmldoc.Load(xmlFile); XmlNodeList list = xmldoc.GetElementsByTagName("Settings"); try { foreach (XmlNode searchNode in list) { return searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText; } } catch (Exception) { } return ""; } public bool setSetting(string settingNode, string newValue) { xmldoc.Load(xmlFile); XmlNodeList list = xmldoc.SelectNodes("Settings"); try { foreach (XmlNode searchNode in list) { XmlNode node = searchNode.SelectSingleNode(settingNode.Replace(" ", "_")); if (node == null) { XmlElement element = xmldoc.CreateElement(settingNode.Replace(" ", "_")); xmldoc.DocumentElement.AppendChild(element); searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText = newValue; } else searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText = newValue; } } catch (Exception) { return false; } xmldoc.Save(xmlFile); return true; } } }
jayfella Reviewed by jayfella on . [c#] XML Reader / Writer Here's a handly little class that i use very often to read/write XML files. Its a very handy little tool to save settings or anything else you wish to read/write often. Hope you find it useful Useage: setting mySettings = new settings("myFile.xml"); // To Retrieve Values Rating: 5
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
[PHP] Twitter Feed Reader + Caching
By Whoo in forum Web Development AreaReplies: 0Last Post: 10th Feb 2012, 06:54 PM -
RSS Reader
By TheLorenalex in forum Technical Help Desk SupportReplies: 1Last Post: 30th Oct 2011, 03:07 AM -
[Buying] RL with Newsgroup reader
By only1_PO in forum Completed TransactionsReplies: 0Last Post: 22nd Oct 2011, 07:00 PM -
Ebook reader and writer
By CatchItBaby in forum Technical Help Desk SupportReplies: 0Last Post: 25th Mar 2011, 03:02 PM -
[TUT] Creating a RSS Reader in WPF in 15 minutes
By Hyperz in forum Web Development AreaReplies: 11Last Post: 27th Jul 2010, 08:10 PM
themaCreator - create posts from...
Version 3.47 released. Open older version (or...