|
-
Jan 15th, 2009, 02:10 PM
#1
Thread Starter
Addicted Member
-
Jan 15th, 2009, 02:41 PM
#2
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
-
Jan 15th, 2009, 02:45 PM
#3
Thread Starter
Addicted Member
-
Jan 15th, 2009, 02:58 PM
#4
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:
Dim writer As New Xml.XmlTextWriter("example.xml", System.Text.Encoding.UTF8)
writer.Formatting = Xml.Formatting.Indented
writer.WriteStartDocument()
writer.WriteStartElement("Records")
writer.WriteStartElement("Record")
writer.WriteAttributeString("Host", String.Empty, "local")
writer.WriteAttributeString("User", String.Empty, "myUsername")
writer.WriteAttributeString("Password", String.Empty, "myPassword")
writer.WriteAttributeString("Directory", String.Empty, "myDirectory")
writer.WriteAttributeString("Timeout", String.Empty, "myTimeout")
writer.WriteAttributeString("URL", String.Empty, "myUrl")
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 15th, 2009, 03:02 PM
#5
Re: [2008] XML or INI
Aw dammit paul I just finished my example! 
vb Code:
Using writer As New XmlTextWriter("C:\Testing\test.xml", System.Text.Encoding.UTF8)
With writer
.WriteStartDocument()
.WriteStartElement("UserProfile")
.WriteElementString("UserName", usernamebox.Text)
.WriteEndElement()
.WriteEndDocument()
.Close()
End With
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.
-
Jan 15th, 2009, 03:08 PM
#6
Thread Starter
Addicted Member
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.
-
Jan 15th, 2009, 03:15 PM
#7
Re: [2008] XML or INI
Here's a code example I gave someone else earlier for reading an XML file:
vb.net Code:
Dim xmldoc As New xmldocument
xmldoc.Load("C:\test.xml")
Dim Nodes As XmlNodeList
Nodes = xmldoc.SelectNodes("/Server/Specs")
For Each node As XmlNode In Nodes
MessageBox.Show(node("HardDrives").InnerText)
Next
Obviously you need to change various bits and bobs in that but it might help you out a bit
-
Jan 15th, 2009, 03:26 PM
#8
Re: [2008] XML or INI
heres how you'd retrieve the xml from my example:
vb Code:
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load("example.xml")
Dim Host As String
Dim User As String
Dim Password As String
Dim Directory As String
Dim Timeout As String
Dim URL As String
For Each node As Xml.XmlNode In xmlDoc.SelectNodes("Record/Records")
Host = node.Attributes("Host").Value
User = node.Attributes("User").Value
Password = node.Attributes("Password").Value
Directory = node.Attributes("Directory").Value
Timeout = node.Attributes("Timeout").Value
URL = node.Attributes("URL").Value
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 15th, 2009, 03:35 PM
#9
Thread Starter
Addicted Member
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
-
Jan 15th, 2009, 03:38 PM
#10
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?
-
Jan 15th, 2009, 03:42 PM
#11
Thread Starter
Addicted Member
Re: [2008] XML or INI
I used Pauls to save and load, still trying to get yours working to load.
-
Jan 15th, 2009, 03:48 PM
#12
Re: [2008] XML or INI
did you get it working now?
if not, can you post your code?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 15th, 2009, 03:50 PM
#13
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?
-
Jan 15th, 2009, 03:57 PM
#14
Thread Starter
Addicted Member
Re: [2008] XML or INI
vb Code:
Imports System.Xml
Public Class frmFTP
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim writer As New Xml.XmlTextWriter("example.xml", System.Text.Encoding.UTF8)
writer.Formatting = Xml.Formatting.Indented
writer.WriteStartDocument()
writer.WriteStartElement("Records")
writer.WriteStartElement("Record")
writer.WriteAttributeString("Host", String.Empty, txtHost.Text)
writer.WriteAttributeString("User", String.Empty, txtUser.Text)
writer.WriteAttributeString("Password", String.Empty, txtPass.Text)
writer.WriteAttributeString("Directory", String.Empty, txtDir.Text)
writer.WriteAttributeString("Timeout", String.Empty, txtTim.Text)
writer.WriteAttributeString("URL", String.Empty, txtUrl.Text)
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
End Sub
Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load("example.xml")
For Each node As Xml.XmlNode In xmlDoc.SelectNodes("Record")
txtHost.Text = node.Attributes("Host").Value
txtUser.Text = node.Attributes("User").Value
txtPass.Text = node.Attributes("Password").Value
txtDir.Text = node.Attributes("Directory").Value
txtTim.Text = node.Attributes("Timeout").Value
txtUrl.Text = node.Attributes("URL").Value
Next
End Sub
End Class
Here you go Thanks, im going to try get chris's working.
-
Jan 15th, 2009, 04:04 PM
#15
Re: [2008] XML or INI
all that i can see wrong with it is:
vb Code:
xmlDoc.SelectNodes("Record")
should be:
vb Code:
xmlDoc.SelectNodes("Records/Record")
chris's example uses the same method
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 15th, 2009, 04:15 PM
#16
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|