Results 1 to 16 of 16

Thread: [RESOLVED] [2008] XML or INI

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Resolved [RESOLVED] [2008] XML or INI

    Hey I have an application that has a user interface like this



    The user will be able to save, edit, make a new account. The problem I am having is if I should use ini files or xml, some people told me that ini files are out of date and shouldn't be used so i started googling xml but i can't find anything useful.

    If it was a ini file i would have it set out like this

    Code:
    [Name]
    user=myusername
    password=mypassword
    etc...

    If xml is better how would i go about doing this?

    Thanks

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] XML or INI

    If it was me I would probably go for XML, and since I just learnt today how to read XML files, I suppose this would be a good chance for me to learn how to write them. I'll post back with my code (assuming I get it working) in a moment
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [2008] XML or INI

    Ok thanks

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008] XML or INI

    you could easily write an xml file
    heres the output:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Records>
      <Record Host="local" User="myUsername" Password="myPassword" Directory="myDirectory" Timeout="myTimeout" URL="myUrl" />
    </Records>
    heres the code to use:

    vb Code:
    1. Dim writer As New Xml.XmlTextWriter("example.xml", System.Text.Encoding.UTF8)
    2. writer.Formatting = Xml.Formatting.Indented
    3. writer.WriteStartDocument()
    4. writer.WriteStartElement("Records")
    5. writer.WriteStartElement("Record")
    6. writer.WriteAttributeString("Host", String.Empty, "local")
    7. writer.WriteAttributeString("User", String.Empty, "myUsername")
    8. writer.WriteAttributeString("Password", String.Empty, "myPassword")
    9. writer.WriteAttributeString("Directory", String.Empty, "myDirectory")
    10. writer.WriteAttributeString("Timeout", String.Empty, "myTimeout")
    11. writer.WriteAttributeString("URL", String.Empty, "myUrl")
    12. writer.WriteEndElement()
    13. writer.WriteEndElement()
    14. writer.Close()

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] XML or INI

    Aw dammit paul I just finished my example!

    vb Code:
    1. Using writer As New XmlTextWriter("C:\Testing\test.xml", System.Text.Encoding.UTF8)
    2.   With writer
    3.         .WriteStartDocument()
    4.         .WriteStartElement("UserProfile")
    5.         .WriteElementString("UserName", usernamebox.Text)
    6.         .WriteEndElement()
    7.         .WriteEndDocument()
    8.         .Close()
    9.    End With
    10. End Using

    EDIT: One thing I do not understand about XML is what is the point in attributes? I mean for example, the way you have done in your example you have just created attributes for each item that is going to be stored, whereas I have created an element/node for each item. Is one method better than the other?
    Last edited by chris128; Jan 15th, 2009 at 03:06 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [2008] XML or INI

    Thanks guys the help is much appreciated, and how would I retrieve the data into the textbox like txtHost.text =

    Edit: I tried reversing the code didn't really work ill have another google or yahoo.
    Last edited by Banjo; Jan 15th, 2009 at 03:13 PM.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] XML or INI

    Here's a code example I gave someone else earlier for reading an XML file:

    vb.net Code:
    1. Dim xmldoc As New xmldocument
    2. xmldoc.Load("C:\test.xml")
    3.  
    4. Dim Nodes As XmlNodeList
    5. Nodes = xmldoc.SelectNodes("/Server/Specs")
    6.  
    7. For Each node As XmlNode In Nodes
    8.      MessageBox.Show(node("HardDrives").InnerText)
    9. Next

    Obviously you need to change various bits and bobs in that but it might help you out a bit
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008] XML or INI

    heres how you'd retrieve the xml from my example:

    vb Code:
    1. Dim xmlDoc As New Xml.XmlDocument
    2. xmlDoc.Load("example.xml")
    3.  
    4. Dim Host As String
    5. Dim User As String
    6. Dim Password As String
    7. Dim Directory As String
    8. Dim Timeout As String
    9. Dim URL As String
    10.  
    11. For Each node As Xml.XmlNode In xmlDoc.SelectNodes("Record/Records")
    12.     Host = node.Attributes("Host").Value
    13.     User = node.Attributes("User").Value
    14.     Password = node.Attributes("Password").Value
    15.     Directory = node.Attributes("Directory").Value
    16.     Timeout = node.Attributes("Timeout").Value
    17.     URL = node.Attributes("URL").Value
    18. Next

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [2008] XML or INI

    Thanks, when I use your code .paul. and do

    Code:
    txtHost.text = node.Attributes("Host").Value
    its just blank even though the data is in the file saved, why is this?

    Thanks

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] XML or INI

    who's method of saving the data did you use? If you saved the data in the way I mentioned (using Nodes instead of Attributes) and are then trying to read it back using paul's method then you will find that it doesnt find anything.

    Also, just to re-ask the question I added to one of my previous posts - does anyone know if using attributes for something like this is better/worse than using nodes or is there no real difference?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [2008] XML or INI

    I used Pauls to save and load, still trying to get yours working to load.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008] XML or INI

    did you get it working now?

    if not, can you post your code?

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] XML or INI

    well mine wont load anything if you used paul's method of saving
    As for why paul's isnt working... not sure, can you post your entire code block?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [2008] XML or INI

    vb Code:
    1. Imports System.Xml
    2. Public Class frmFTP
    3.  
    4.     Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    5.         Dim writer As New Xml.XmlTextWriter("example.xml", System.Text.Encoding.UTF8)
    6.         writer.Formatting = Xml.Formatting.Indented
    7.         writer.WriteStartDocument()
    8.         writer.WriteStartElement("Records")
    9.         writer.WriteStartElement("Record")
    10.         writer.WriteAttributeString("Host", String.Empty, txtHost.Text)
    11.         writer.WriteAttributeString("User", String.Empty, txtUser.Text)
    12.         writer.WriteAttributeString("Password", String.Empty, txtPass.Text)
    13.         writer.WriteAttributeString("Directory", String.Empty, txtDir.Text)
    14.         writer.WriteAttributeString("Timeout", String.Empty, txtTim.Text)
    15.         writer.WriteAttributeString("URL", String.Empty, txtUrl.Text)
    16.         writer.WriteEndElement()
    17.         writer.WriteEndElement()
    18.         writer.Close()
    19.     End Sub
    20.  
    21.     Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
    22.         Dim xmlDoc As New Xml.XmlDocument
    23.         xmlDoc.Load("example.xml")
    24.  
    25.         For Each node As Xml.XmlNode In xmlDoc.SelectNodes("Record")
    26.             txtHost.Text = node.Attributes("Host").Value
    27.             txtUser.Text = node.Attributes("User").Value
    28.             txtPass.Text = node.Attributes("Password").Value
    29.             txtDir.Text = node.Attributes("Directory").Value
    30.             txtTim.Text = node.Attributes("Timeout").Value
    31.             txtUrl.Text = node.Attributes("URL").Value
    32.         Next
    33.  
    34.     End Sub
    35. End Class

    Here you go Thanks, im going to try get chris's working.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [2008] XML or INI

    all that i can see wrong with it is:

    vb Code:
    1. xmlDoc.SelectNodes("Record")

    should be:

    vb Code:
    1. xmlDoc.SelectNodes("Records/Record")

    chris's example uses the same method

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [2008] XML or INI

    Ahh thanks i changed that to see if it would work, I remember now i changed
    Code:
    writer.WriteStartElement("Record")
    in the write code just testing something and forgot to change back. Thankyou chris and paul for the help

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