|
-
Jun 17th, 2008, 08:15 AM
#1
Thread Starter
Hyperactive Member
[2005] Best practices when saving my application's project data.
I am currently writing an application that accepts N number of files that will be processed. For each file, the user will be able to manipulate about 30 different pieces of information. Of course, I am allowing the user to save these files as one project, so they can re-load it for later use.
Right now, I am saving this information as a "txt" file. Here is what I am doing:
1) Line 1, I write "Project=MyProject". This is what I am using as one of the way to verify that the "txt" file is a genuine project made in my application.
2) For each subsequent line, I write out each individual file and its 30 pieces of information separated by the "|" symbol.
I want to make sure I am doing this the way most here would do it. Is there a better more efficient way to save project data, or is what I am doing fall in line with what people here would do?
-
Jun 17th, 2008, 08:18 AM
#2
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
you could also create a class, make it Serializable, create a new object of this, with all the imformation an save it as i file ... that a pretty easy way of doing it..
or maybe save it as xml.
-
Jun 17th, 2008, 08:31 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
 Originally Posted by gnaver
you could also create a class, make it Serializable, create a new object of this, with all the imformation an save it as i file  ... that a pretty easy way of doing it..
or maybe save it as xml.
Hmmm, never thought about using xml before. The app is almost finished but I may have to look into that.
As far as a class, I thought extremely hard about that but I was a little worried about passing all those objects around in memory, but that may have been inconsequential as I will admit I am not sure about that aspect.
Right now the data is being saved in a Datatable, which may or may not have been the same as objects.
-
Jun 17th, 2008, 08:33 AM
#4
Thread Starter
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
I will do some research on my own, but would I make the XML file by writing the elements out to a file or is there a .Net class that would help me make the XML structure?
-
Jun 17th, 2008, 09:11 AM
#5
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
maybe you can use the xmltextWriter class, (Xml.XmlTextWriter)
-
Jun 17th, 2008, 09:34 AM
#6
Thread Starter
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
Quick question, I found this example:
http://www.java2s.com/Code/VB/XML/Bu...MLDocument.htm
Unfortunately, it doesn't show how to write it out to a file. Based off of the example above is this the best method to write the data to a file:
Code:
'ms = MemoryStream Object
'make file
fr = New StreamReader(ms)
ms.Seek(0, SeekOrigin.Begin)
fw = New StreamWriter(FileName, False)
fw.Write(fr.ReadToEnd())
fw.Close()
fr.Close()
I don't really like opening up a Stream-reader/writer, so I am wondering if there is a way to just use the streamwriter.
-
Jun 17th, 2008, 10:01 AM
#7
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
Dim xml_text_writer As New Xml.XmlTextWriter("c:\yourfile.xml", System.Text.Encoding.ASCII)
-
Jun 17th, 2008, 10:08 AM
#8
Thread Starter
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
gnaver, thanks I will try that out.
I have one other question for you. It is a basic XML question regarding empty values.
1) When I have value for a an element, it writes:
<Element> </Element>
2) When I have an empty value, it writes:
<Element />
When I trying to retrieve this element back, will this prevent me from getting the data if it is in form #2? If so, is there a way to write it out like #1 everytime?
-
Jun 17th, 2008, 10:35 AM
#9
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
you cant retrieve that element as it is empty :S...
but you can stil read the xml file whit no problems..
-
Jun 17th, 2008, 10:53 AM
#10
Re: [2005] Best practices when saving my application's project data.
the second option there is the prefered way to write out an empty xml element.
Ok, here's how I'd do it: Create a class that contains all of the properties that you need to keep track of per project. Don't worry about passing around "all those objects" ... an object never gets passed, only a pointer (an address if you like) gets passed, and that's a lot smaller than say an array or a dozen string variables.
Next, I'd make the class serializable. That's as simple as adding the serializable namespace to the class, then decorating the class with the <serializable> tag.
Next thing to do would be to write a ReadProject and a WriteProject routine. At this point you have two choices - to write the data out in an XML file or write it out in binary format. Binary has the advantage of potentially being smaller, also since it write binary out, the file isn't easily readable except by your application. XML on the other had will be slightly larger (it is verbose after all) but it can also be modified in something as simple as notepad.
What you'll want to do at this point is to read up on XMLSerialization and BinarySerialization and decide which you want, then write the code (about 3-5 lines to write it to file, and 3-5 to read it back in). By using this method you don't have to parse anything, just write the file and read the file.
In either case, you can also give the files a new extension that you can then associate with your application, so that if a user double clicks it in file explorer, it opens your app then loads the file.
-tg
-
Jun 17th, 2008, 12:21 PM
#11
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
to save a class to binary:
vb Code:
Private sub SaveYourClass(ByVal obj2Save As YourClass, ByVal fileName As String) As Boolean
Try
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim mem As New IO.MemoryStream
bf.Serialize(mem, obj2Save)
My.Computer.FileSystem.WriteAllText(fileName, Convert.ToBase64String(mem.ToArray()), False)
Catch ex As ApplicationException
MessageBox.Show(ex.Message)
End Try
End Sub
To load the binary again:
vb Code:
Private Function LoadClass(ByVal fileName As String) As YourClass
Try
Dim File As String = My.Computer.FileSystem.ReadAllText(fileName)
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim mem As New IO.MemoryStream(Convert.FromBase64String(File))
Return (DirectCast(bf.Deserialize(mem), YourClass))
Catch ex As ApplicationException
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
-
Jun 17th, 2008, 12:34 PM
#12
Thread Starter
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
Question, as I told you all before, I have a datatable and each row has the information for the file, could I write that Datatable as binary?
Also, I have always been curious about file extensions. Do you have to make sure there isn't an extension that already exists? For example, if I wanted to use ".mdg", do I check and see if that is registered with a software app?
-
Jun 17th, 2008, 12:39 PM
#13
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
you can save your datatable as binary ... use whatever you like, maybe dont use exe,bat etc.
-
Jun 17th, 2008, 02:42 PM
#14
Thread Starter
Hyperactive Member
Re: [2005] Best practices when saving my application's project data.
It's official, you all are geniuses. I used the save/load binary functions with slight modifications and it worked like a charm.
I was extremely uneasy about doing it the way I described in the OP, so thank you all for helping me do things in a more efficient manner.
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
|