![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Expert Programmer
|
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 FunctionSerialization 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. |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#13 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
|
|
#14 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 4
![]() |
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 .) |
|
|
|
|
|
#15 | |
|
Expert Programmer
|
Quote:
![]() 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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|