Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 5th, 2006, 4:30 PM   #11
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Ok a quick and dirty way to hibernate objects in an XML file is through serialization, and you don't even need metadata or reflection as you know what they are.

Imports System.Runtime.Serialization.Formatters
...
    Public Sub LoadSomething (ByRef SomeInputBuffer as SomeType)
            Dim XMLSerializer As Soap.SoapFormatter = New Soap.SoapFormatter
            Dim XMLStream As FileStream = New FileStream("C:\somefile.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            SomeInputBuffer = CType(XMLSerializer.Deserialize(XMLStream), SomeInputType)
            XMLStream.Close()
    End Sub
    Public Sub SaveSomething (ByRef SomeOutputBuffer as SomeType)
            Dim XMLSerializer As Soap.SoapFormatter = New Soap.SoapFormatter
            Dim XMLStream As FileStream = New FileStream("C:\somefile.xml", FileMode.Create, FileAccess.Read, FileShare.Read)
            XMLSerializer.Serialize(XMLStream, SomeOutputBuffer)
            XMLStream.Close()
    End Function

Serialization will definetely work with all the built-in data classes you could be interested in this context, and works for many others too. If you want a class to be serializable you need to mark it <Serializable()>_ (duh) and possibly write some serializing routines for it.

However I stress this is not a robust system in terms of efficiency or security, the serializer will deserialize anything, including potentially malicious objects in which case you'd need to put in the effort and parse it yourself.
Rory is offline   Reply With Quote
Old Jan 5th, 2006, 5:06 PM   #12
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 4 emdiesse is on a distinguished road
I'm beginning to think that for now I should complete my Computing Project using Access and VB rather than this. I can then start doing things like this when I become more confident with VB.NET.

Is that a good idea, or should I battle it out?

Assuming I have absolutely no knowledge (I know little bits) would a project of this scale be accomplisable in about 5 weeks?
emdiesse is offline   Reply With Quote
Old Jan 5th, 2006, 5:25 PM   #13
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,618
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
I think you could finish in 5 weeks but it will take alot of work to get it done in that amount of time if your still learning the language.
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Jan 5th, 2006, 5:35 PM   #14
emdiesse
Programmer
 
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 4 emdiesse is on a distinguished road
Ah. Thats nice to hear. How many hours do you think were talking about here? Baring in mind I have exams coming up next week, but after that none til june.

(I would love to get this all done in VB.NET will really help me when I go to uni because I will have more knowledge of a higher level language .)
emdiesse is offline   Reply With Quote
Old Jan 6th, 2006, 9:05 AM   #15
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Quote:
Originally Posted by emdiesse
(I would love to get this all done in VB.NET will really help me when I go to uni because I will have more knowledge of a higher level language .)
Uni will no doubt consist of Hakell, Java and possibly some C

Sorry if my post was confusing; basically the simplest way to store data in a program without using a DBMS like MySQL is in objects, also encouraging good Object Oriented practices - XML Serialization is merely a convenient way to save and load them from disk, with the advantage that unlike conventional Binary Serialization, XML files are human readable.

For instance the following console app demonstrates saving and reloading a hashtable to and from a file:

' N.B. You'll need to add a reference to System.Runtime.Serialization.Formatters in your project
Option Explicit On 
Option Strict On
Imports System.Data
Imports System.IO
Imports System.Runtime.Serialization.Formatters
Module modMain

    Sub Main()
        Dim CapitalCities As Hashtable = New Hashtable
        Dim InputBuffer As Object

        With CapitalCities 'Enter some data into the hashtable
            .Add("England", "London")
            .Add("France", "Paris")
            .Add("Japan", "Tokyo")
            .Add("USA", "Washington")
            .Add("Liechtenstein", "Vaduz")
        End With

        ' Save it to disk
        SaveSomething(CType(CapitalCities, Object), Directory.GetCurrentDirectory() & "CapitalCities.xml")

        ' Clear out the object to prove it's being reloaded from disk
        CapitalCities = Nothing

        ' Load hashtable from disk
        LoadSomething(InputBuffer, Directory.GetCurrentDirectory() & "CapitalCities.xml")

        CapitalCities = CType(InputBuffer, Hashtable) ' We needed some memory to write to
        InputBuffer = Nothing

        Dim CapitalCitiesEnum As IDictionaryEnumerator = CapitalCities.GetEnumerator
        With CapitalCitiesEnum
            Do While .MoveNext
                ' List contents
                Console.WriteLine("The capital of " & CType(.Key, String) & " is " & CType(.Value, String))
            Loop
        End With

        Console.Write("Press enter...") : Console.ReadLine()
    End Sub

    Public Sub LoadSomething(ByRef SomeInputBuffer As Object, ByVal FilePath As String)
        Dim XMLSerializer As Soap.SoapFormatter = New Soap.SoapFormatter
        Dim XMLStream As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Try
            SomeInputBuffer = XMLSerializer.Deserialize(XMLStream)
        Finally
            XMLStream.Close()
        End Try
    End Sub
    Public Sub SaveSomething(ByRef SomeOutputBuffer As Object, ByVal FilePath As String)
        Dim XMLSerializer As Soap.SoapFormatter = New Soap.SoapFormatter
        Dim XMLStream As FileStream = New FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)
        Try
            XMLSerializer.Serialize(XMLStream, SomeOutputBuffer)
        Finally
            XMLStream.Close()
        End Try
    End Sub
End Module
Rory is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC