Results 1 to 10 of 14
-
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
-
16th Jun 2010, 10:43 PM #2MemberWebsite's:
litewarez.net litewarez.com triniwarez.comim still noob at this, put can you directly use the XML Namespace to allocate the settings class ?
eg:
PHP Code:namespace System.Xml
{
class jaysParser
{
/*....*/
}
}
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
16th Jun 2010, 10:45 PM #3OPლ(ಠ益ಠლ)Website's:
extremecoderz.comyep.
you can either do:
using Jayfella.XMLParser;
in the top with the rest, or just
Jayfellla.XMLParser.Settings mySettings = new Settings("myFile.xml")
Either way works.
-
16th Jun 2010, 10:48 PM #4MemberWebsite's:
litewarez.net litewarez.com triniwarez.comyea its ok i thought to myself as i like ordered code that you should extend the xmlParser to the XML Namespace but wasn't sure if your allowed to implement user namepaces into a system namespace :/
Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
16th Jun 2010, 10:56 PM #5Respected Developer
Nice example. I personally would remove all try-catch blocks though .
Also, who can spot what's wrong with:
PHP Code: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 "";
}
Edit:
Xdocument + Linq queries > XmlDocument.
-
16th Jun 2010, 10:59 PM #6MemberWebsite's:
litewarez.net litewarez.com triniwarez.comthe parametres are incorrect ?
searchNode should be settingNode
XmlNode should be XmlNodeListJoin Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
16th Jun 2010, 11:02 PM #7Respected Developer
Nope lite . It's a design mistake I'm looking for.
-
16th Jun 2010, 11:02 PM #8MemberWebsite's:
litewarez.net litewarez.com triniwarez.comwut !
/8char
Edit:
foreach (XmlNode searchNode in list)
list is a special operator and not a variable! in defines what variable it is traversing so it should be
foreach (searchNode in XmlNode)
or you should not be using list in the above variable setter
//maybeeeJoin Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
16th Jun 2010, 11:03 PM #9OPლ(ಠ益ಠლ)Website's:
extremecoderz.comI try/catched it for a reason, i forget why now.. lemme think.
oh oh - user account control - its not enough to try/catch it normally, it fails inside the class if the program is denied write permissions.
EDIT: Yeah i know, no catch routine. so shoot me
-
16th Jun 2010, 11:06 PM #10Respected Developer
I know jay, but you don't need to try-catch there, you need to if-else.
Anyways:
PHP Code:foreach (XmlNode searchNode in list)
{
return searchNode.SelectSingleNode(settingNode.Replace(" ", "_")).InnerText;
}
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.24 released. Open older version (or...