Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / XML
Article

EasyData - A simple and fast way to work with data

Rate me:
Please Sign up or sign in to vote.
3.36/5 (8 votes)
11 Apr 20054 min read 49.2K   290   30   3
This code shows a very simple way to work with data, using XML serializer, and a little trick with ArrayLists.

Image 1

Introduction

I'll try to explain (if my poor English permits) the way I currently work with data, specially when I need to do this as fast as possible: design-create-fill-save-and-load data. I found this a good way if you don't have time or you don't want to work with SQL Server or Access databases. Into the article, I'll explain some interesting topics we'll need to accomplish the goal, how to create what we can call a DataClass, the serializing-n-deserializing tool (it comes with VB!), and a tricky tip on array serializing.. With this, we can make the EasyData work.

Background

Some time ago, I came here to find some code on XML saving and I had some problem trying to serialize my classes. I soon found that the XML serializer does not work with an ArrayList. What to do now? I wanted to save my data without complication and into some pattern that would be easy to be "remembered" (that's why I chose XML), but I didn't want to lose the great facilities that the ArrayLists give when you're a beginner to the data world. So that's the beginning of EasyData.

Using the code

Let's start from the beginning. The first pass is to create an easy and fast way to work with data. I found ArrayList very useful and simple to do this. Let's see the two classes:

VB
'This is the main class
Public Class EasyData
    'create an Table-ArrayList
    Private Table As New ArrayList
End Class

'This is the data class
Public Class SingleData
    'here you can insert as many fields as you want.
    Public FirstInfo As String
    Public SecondInfo As String
    Public ThirdInfo As String
End Class

Explaining: the SingleData class is where we actually insert the data into. The EasyData class is where the whole fun is. As Table is an ArrayList, it's easy to add and remove objects from it. No connections needed, no need of start/end reading. The result is that it became an unusual way to manage large sets of data. (Well, if you're thinking of 1000 K lines, consider SQL or Access!)

VB
'Return the item x from the table
Public Function Item(ByVal x As Short) As SingleData
    Return Table.Item(x)
End Function

'Return how many items are into the table
Public Function Count() As Short
    Return Table.Count
End Function

'Add one item to the array
Public Sub Add(ByVal SD As SingleData)
    Table.Add(SD)
End Sub

'Remove item from the array
Public Sub Remove(ByVal SD As SingleData)
    Table.Remove(SD)
End Sub

I found that the functions Add, Remove, Count and Item are enough to manage the data.

Save Trick

So we made one table and we can work with the data. And now? How to save the information?

This was my question when I came here and I found some interesting articles on how to serialize classes in VB.NET and an overall view in "XML Data Files, XML Serialization, and .NET". Then to the classic Ctrl+C and Ctrl+V. And... it didn't work. ArrayList has an advantage, you don't need to size the array and you don't need to bother about the way data is stored inside it. It is great for beginners or for a fast data application, but as it "doesn't have" a size neither an internal order, we cannot serialize it. Then I got some simple idea. Well, when I need to work with data, I'll use ArrayList, and while saving, I'll use a normal vector and then make one XML file during serialization.

In order to maintain some learning aspect, I've separated the functions of saving/loading and converting, into one class: FileFunction. Here is the main aspect of the program:

Image 2

Picture 2 - An overview of the saving process (the Load is analog).

VB
'convert the Table-ArrayList into one vector SingleData()
Function ArrayToList(ByVal Table As ArrayList) As SingleData()
    Dim i As Short
    Dim p(Table.Count) As SingleData
    For i = 0 To ed.Count - 1
        p(i) = ed.Item(i)
    Next
    Return p
End Function

Here we have the "implemented functions" shown on Picture 2. The function is simple, get one ArrayList and create a SingleData vector p with Table.Count elements. Then all we need is to fulfill the vector with the data from the Array.

VB
'Get one Table-ArrayList and save it into one xml file
Public Sub Save(ByVal filename As String, ByVal ED As ArrayList)
    Dim SD() As SingleData
    SD = ArrayToList(ED)
    Dim serializer As New XmlSerializer(SD.GetType)

    ' Create an XmlSerializerNamespaces object.
    Dim ns As New XmlSerializerNamespaces
    ' Create an XmlTextWriter using a FileStream.
    Dim fs As New FileStream(filename, FileMode.Create)
    Dim writer As New XmlTextWriter(fs, New UTF8Encoding)
    ' Serialize using the XmlTextWriter.
    serializer.Serialize(writer, SD, ns)
    writer.Close()
End Sub

Here is the save process. Its' simple. If you have any doubt, may be a first look on "XML Data Files, XML Serialization, and .NET" may help a lot. But the main work is simple. The function gets the Table-ArrayList and uses the function ArraytoList so create an exact copy but in a Vector form, so no more trouble. We create one object responsible to serialize and a writer that is the responsible for creating the XML archive itself.

The load process is identical to the saving one, I think I don't need to explain because it is really simple and is commented on the source code.

Demo project data work

In the demo, I made a simple way to work with the data. The rule here is to be simple. I'd put some textboxes to input the data and one listbox to visualize the data. The other four buttons are just Add, Delete, Save and Load.

Points of Interest

Some may see this code as completely useless, I may agree with you but it really saves a lot of time, and after all, I learned a lot from it! That's the fun!

Lots of improvements are needed. Maybe the first thing would be velocity, maybe changing some functions. As soon as I get something new, I'll edit this topic.

If any of make some improvements to this, send an e-mail to me rmaax@ig.com.br. I'll be glad to see someone out there like this crazy thing too.

Oh and as it is, it's almost ready to use as one phones or emails data.

History

First version online.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Brazil Brazil
Control and Automation Student.
Working with VB.Net in the past 2 years.
Loves microeletronic and AI.

Comments and Discussions

 
GeneralQuestion >.< Pin
Anonymous1-Aug-05 16:51
Anonymous1-Aug-05 16:51 
GeneralHmmm Pin
John Korres29-Jun-05 1:21
John Korres29-Jun-05 1:21 
GeneralArrayToList etc Pin
Tom Janssens11-Apr-05 21:26
Tom Janssens11-Apr-05 21:26 
Hi,

A small suggestion : ArrayList has functions to convert from & to an array (code in c#):

[code=c#]
//ArrayList 'Al' contains 'SingleItem'

//convert Arraylist to array of SingleItem
SingleItem[] si = Al.ToArray(TypeOf(SingleItem));

// add SingleItem[] to ArrayList
Al.Add(si);
[/code]

Makes your code more compact.

Tom

Core

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.