Results 1 to 26 of 26

Thread: XML Galore: Replacement for INI files

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    XML Galore: Replacement for INI files

    here are some functions that might come in handy

    here is XML file before changes
    Code:
    <?xml version="1.0" standalone="no"?>
    <ProgramSettings>
      <Lingo>German</Lingo>
    </ProgramSettings>
    here are 2 lines of code, one to set a new value, and the other to read the value..

    Code:
    MessageBox.Show(myXML.setXMLNodeInXMLFile(Environment.CurrentDirectory + @"\settings.xml", "ProgramSettings", "Lingo", "Spanish"));
    MessageBox.Show(myXML.getXMLNodeFromXMLFile(Environment.CurrentDirectory  + @"\settings.xml", "ProgramSettings", "Lingo"));
    here is the result
    Code:
    <?xml version="1.0" standalone="no"?>
    <ProgramSettings>
      <Lingo>Spanish</Lingo>
    </ProgramSettings>
    and here is the code

    PHP Code:
    //Descrtiption:        All functions and methods related to XML 
    //File:            myXML.cs
    //Author Name:        Kovan Abdulla    
    //Author Email:        [email][email protected][/email]    
    //Author Homepage:    [url]http://www.imetasoft.com[/url]
    using System;
    using System.Xml;
    namespace 
    CSharpDummyApp
    {

        public class 
    myXML
        
    {
            private 
    myXML()
            {
            }

            public static 
    string getXMLNodeFromXMLFile(    string xmlFile
                                                        
    string parentNode
                                                        
    string node)
            
    //PURPOSE: To retrieve the text between an xml tag from a xml file
            //REQUIRED: the node or the text between xml tag and the parent of that node 
            //PROMOSE: return either file not found or the text of the given node
            
    {
                
                try
                {
                    
    XmlDocument doc = new XmlDocument(); // get an XML document
                    
    doc.Load(xmlFile); //load the file
                    
    XmlNodeList xmlNode doc.GetElementsByTagName(node);
                    for(
    int i 0xmlNode.Counti++) //loop through all the found nodes
                    
    {
                        if (
    xmlNode[i].ParentNode.Name == parentNode//find the one we want in the collection
                        
    {            
                            return 
    xmlNode[i].InnerText.ToString(); //return the text
                        
    }
                    }
                    return 
    "Node belonging to " parentNode " was not found in " xmlFile;
                }
                catch(
    System.Exception e//catch the exception such as if the file passed in is not found
                
    {
                    return 
    e.Message;
                    
                }
            }


            public static 
    string setXMLNodeInXMLFile(    string xmlFile
                                                        
    string parentNode
                                                        
    string node
                                                        
    string nodeValue)
            
    //PURPOSE: To set the text between an xml tag in a xml file
            //REQUIRED: the node or the text between xml tag and the parent of that node, and new text 
            //PROMOSE: return either file not found or success message
            
    {
                
                try
                {
                    
    XmlDocument doc = new XmlDocument(); // get an XML document
                    
    doc.Load(xmlFile); //load the file
                    
    XmlNodeList xmlNode doc.GetElementsByTagName(node);
                    for(
    int i 0xmlNode.Counti++) //loop through all the found nodes
                    
    {
                        if (
    xmlNode[i].ParentNode.Name == parentNode//find the one we want in the collection
                        
    {            
                            
    xmlNode[i].InnerText nodeValue//set the text
                            
    doc.Save(xmlFile); //save the file back
                            
    return "Node " node "'s value has been changed to " nodeValue " succesfuly";

                        }
                    }
                    return 
    "Node belonging to " parentNode " was not found in " xmlFile;
                }
                catch(
    System.Exception e//catch the exception such as if the file passed in is not found
                
    {
                    return 
    e.Message;
                    
                }
            }
        }


  2. #2
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165
    excellent, thanks kovan!

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    how is the coding style? comments? and so on
    i will be posting a lot of stuff like this in the future...

    so i would like people to undrestand it
    hehe

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    excellent job kovan.

    you da man!
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165
    i managed to convert that code in to vb.net, and got it to work, although i'm having problems reading from an xml file that's structured like this:

    Code:
    <?xml version="1.0" standalone="no"?>
    <Users>
      <User1>
        <Category1>
          <Account1 Username="blah" Password="mypass1" />
          <Account2 Username="abc123" Password="mypass2" />
        </Category1>
        <Category2>
          <Account1 Username="dfgs" Password="dfgdrtgre" />
        <Category2>
      </User1>
      <User2>
        <Category1>
          <Account1 Username="ertret" Password="cvbghgf" />
        </Category1
      </User2
    </Users>
    first of all, it would seem that you can't have two parent nodes with the same name, even if they have seperate parent nodes themselves. secondly, i haven't a clue how to read/write to that. can anyone help?

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    actually i have that on my list
    more functions will be written to deal with such structure
    i just havent coded them yet,
    next release will contain more functions and probably alternative ways of doing things

  7. #7
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165
    *pats kovan on the back

    keep up the good work man!

  8. #8
    Hyperactive Member thinktank2's Avatar
    Join Date
    Nov 2001
    Location
    Arctic
    Posts
    272
    Originally posted by donut
    i managed to convert that code in to vb.net, and got it to work, although i'm having problems reading from an xml file that's structured like this:

    Code:
    <?xml version="1.0" standalone="no"?>
    <Users>
      <User1>
        <Category1>
          <Account1 Username="blah" Password="mypass1" />
          <Account2 Username="abc123" Password="mypass2" />
        </Category1>
        <Category2>
          <Account1 Username="dfgs" Password="dfgdrtgre" />
        <Category2>
      </User1>
      <User2>
        <Category1>
          <Account1 Username="ertret" Password="cvbghgf" />
        </Category1
      </User2
    </Users>
    first of all, it would seem that you can't have two parent nodes with the same name, even if they have seperate parent nodes themselves. secondly, i haven't a clue how to read/write to that. can anyone help?

    <?xml version="1.0" standalone="no"?>
    <Users>
    <User1>
    <Category1>
    <Account1 Username="blah" Password="mypass1" />
    <Account2 Username="abc123" Password="mypass2" />
    </Category1>
    <Category2>
    <Account1 Username="dfgs" Password="dfgdrtgre" />
    <Category2>
    </User1>
    <User2>
    <Category1>
    <Account1 Username="ertret" Password="cvbghgf" />
    </Category1>
    </User2>
    </Users>


  9. #9
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165
    typo...

  10. #10
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    Kovan,

    You should comment your code differently.
    For example, the following method:

    public static string getXMLNodeFromXMLFile(string xmlFile,
    string parentNode,
    string node)


    Try to use:

    /// <summary>
    ///
    /// </summary>
    /// <param name="xmlFile"></param>
    /// <param name="parentNode"></param>
    /// <param name="node"></param>
    /// <returns></returns>


    Not:

    //PURPOSE: To retrieve the text between an xml tag from a xml file
    //REQUIRED: the node or the text between xml tag and the parent of that node
    //PROMOSE: return either file not found or the text of the given node


    Typing 3 slashes (///) above a method or a class will generate the xml style comment. Based on these XML comments VS can generate XML / HTML comment files.
    (Note: only works with C#)

    Tip: if you take a look at gotdotnet there is a pretty cool (!) XSLT which generates the exact same help/documentation as VS it self based on your own XML inline comment.

  11. #11
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346
    Basically, if i recall correctly, when you do the /// it makes documents based off the comments and such. Kinda sorta like javadoc that java creates (if your familure at all with java.) I wish VB.NET had this feature....
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  12. #12
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346

    Smile

    excellent job kovan, i took your C# code and transformed it into VB.NET, which is easily doable, and it works like a charm. I have been looking for something similar to this, and this just hit the nail on the head... Thanks for all your effort and work on this.
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    if you waited, i could have transformed it in to vb you know.. but you didnt want to wait.. hehe

    actually could you send me your vb copy? i will put it inmy collection

    oh and FYI, java sucks (and yes i programmed in it)

  14. #14
    Hyperactive Member kleptos's Avatar
    Join Date
    Aug 2001
    Location
    The Dark Carnival
    Posts
    346
    I am VERY impatient when it comes to code, i would spend weeks converting something rather then waiting, i am wierd like that, besides, C# and VB.NET is close to the same, a {, ; ,} here and there and you've got it. I will send you over my VB code adaptation as soon as i fire up my laptop.
    ..::[kleptos]::..
    • Database Administrator (MSSQL 2000)
    • Application Developer (C#)
    • Web Developer (ASP.NET)


  15. #15
    Junior Member
    Join Date
    Mar 2002
    Location
    trinsic
    Posts
    21

    post the vb code?

    could you post the vb version of this?

  16. #16
    Member AMDPwred's Avatar
    Join Date
    Mar 2002
    Location
    Richmond, VA
    Posts
    48
    What do I need to do to run this code? I created a new C# project and pasted in your code. And I created a XML file with..

    Code:
    <?xml version="1.0" standalone="no"?>
    <ProgramSettings>
      <Lingo>German</Lingo>
    </ProgramSettings>
    ..which was saved in the root directory of my project. What else would I need to do to have it run? I'd like to play around with the source.

  17. #17
    Member AMDPwred's Avatar
    Join Date
    Mar 2002
    Location
    Richmond, VA
    Posts
    48
    Originally posted by kovan
    oh and FYI, java sucks (and yes i programmed in it)
    Any reason for that?

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    Originally posted by AMDPwred
    What do I need to do to run this code? I created a new C# project and pasted in your code. And I created a XML file with..

    Code:
    <?xml version="1.0" standalone="no"?>
    <ProgramSettings>
      <Lingo>German</Lingo>
    </ProgramSettings>
    ..which was saved in the root directory of my project. What else would I need to do to have it run? I'd like to play around with the source.
    all you need is those functions *and the namepsaces that come with it*
    and an xml file to read and write to/from thats all


    as for java sucking.. i find it very very complicated to do the simplist thing, if you created jsp pages and ejb's then you will know what i am talking about

  19. #19
    Member AMDPwred's Avatar
    Join Date
    Mar 2002
    Location
    Richmond, VA
    Posts
    48
    Originally posted by kovan
    as for java sucking.. i find it very very complicated to do the simplist thing, if you created jsp pages and ejb's then you will know what i am talking about
    Yeah I have. I don't see JSPs very hard at all, EJBs yes, but not JSPs. I guess it just depends on the application at hand. I'm just diving into EJBs so I can't speak too much about those.

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    i see SOME usefulness in eGayb's ( )
    as for GayAsp, the only thing that makes them look attractive is the custom tags,
    did i mention Gayasps are very slow
    asp.net is gonna blow them out of the water,
    yes i see there needed to be a better solution than ASP
    Gayasp's somewhat improved on that, but ASP.NET came back with enough army to take over the world...

  21. #21
    Member AMDPwred's Avatar
    Join Date
    Mar 2002
    Location
    Richmond, VA
    Posts
    48
    I like the way JSPs and Servlets work. I seem to get alot done in that cycle.

  22. #22

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    coding them is kinda easy
    but setting them up to work properly for deployment is HELL
    getting to know what order you have to reference different jar files makes it hell, and not to mention there is not one single editor out there that gives helpful error messages
    *jbuilder 5+ is some what good*
    what am trying to say is, MS makes things so much easier for developers
    i guess thats why java developers are so expensive, cus they go through hell
    i hope i never EVER program in java AGAIN

  23. #23
    Member AMDPwred's Avatar
    Join Date
    Mar 2002
    Location
    Richmond, VA
    Posts
    48
    As for the confusion on deployment, XML config files and so forth makes life very easy. I created a bunch of modules in JSP/Servlets a little while back for our company website (still not into production yet) and just packaged the entire site into one WAR file. I'd say the packaging system for Java is pretty helpful if you know how to use it correctly.

    I agree on JBuilder. I couldn't get by without my copy of JBuilder 5.

    Yeah, MS makes things easier. But that doesn't always make things better. I think each language shines in its own way. Although C# seems to have really gone far to appeal to Java developers.

  24. #24

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    i liked the idea of war file and yoru done
    but in order to get that war file to work was a hell
    example of what i am talking about
    i was experimenting with outputing my results in xml format
    using Xerces and Xalan (couldnt find anything better)
    no one told me or no one did i READ that you must have specific ordering of referencing libraries in order to get these to work properly
    thats just one of the examples of a nightmare with java
    sun has made java go down the hill, an example of this is the fact they refused to join the Web Service standard (i believe virtually ALL companies are part of this standard except sun) thats a blow to java
    i personally only work with java when i am TOLD i have to
    a lot of java developers phasing out java and looking into c#

  25. #25
    Member AMDPwred's Avatar
    Join Date
    Mar 2002
    Location
    Richmond, VA
    Posts
    48
    All the Java guys I work with don't care too much about the .NET deal at the moment. They've all got huge salaries coding Java so I can't blame them.

  26. #26

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    ya, thats the problem for ms, java coders (the ones that get a lot of money) are loyal to java and probably the paycheck is whats stoping them from shifting, same with c++ programmers, they get a lot of money for something they have mastered

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width