Activity Stream
48,167 MEMBERS
61007 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1.     
    #1
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com

    Default [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

  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    im 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


  4.     
    #3
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    yep.

    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.

  5.     
    #4
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    yea 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


  6.     
    #5
    Respected 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.

  7.     
    #6
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    the parametres are incorrect ?


    searchNode should be settingNode
    XmlNode should be XmlNodeList
    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


  8.     
    #7
    Respected Developer
    Nope lite . It's a design mistake I'm looking for.

  9.     
    #8
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    wut !

    /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
    //maybeee
    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


  10.     
    #9
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    I 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

  11.     
    #10
    Respected 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;
                    } 
    Why start a loop when you have a return statement in the 1st iteration ?

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [PHP] Twitter Feed Reader + Caching
    By Whoo in forum Web Development Area
    Replies: 0
    Last Post: 10th Feb 2012, 06:54 PM
  2. RSS Reader
    By TheLorenalex in forum Technical Help Desk Support
    Replies: 1
    Last Post: 30th Oct 2011, 03:07 AM
  3. [Buying] RL with Newsgroup reader
    By only1_PO in forum Completed Transactions
    Replies: 0
    Last Post: 22nd Oct 2011, 07:00 PM
  4. Ebook reader and writer
    By CatchItBaby in forum Technical Help Desk Support
    Replies: 0
    Last Post: 25th Mar 2011, 03:02 PM
  5. [TUT] Creating a RSS Reader in WPF in 15 minutes
    By Hyperz in forum Web Development Area
    Replies: 11
    Last Post: 27th Jul 2010, 08:10 PM

Tags for this Thread

BE SOCIAL