3

i have got my application to read in some values from my xml document however i am unsure of how I'm going to store them as i have in total 6 pieces of information for each element in the file at the moment.

XML Example

<?xml version="1.0" encoding="utf-8"?>
<App>
 <Name>First Application </Name>
 <FileName>1.exe</FileName>
 <FilePath>C:\</FilePath>
 <Third_Parameter>etc</Third_Parameter>
 <Forth_Parameter>etc</Forth_Parameter>
 <Name>Application 2</Name>
 <FilePath></FilePath>
 <Third_Parameter>etc</Third_Parameter>
 <Forth_Parameter>etc</Forth_Parameter>
</App>

I was thinking of a array with an unique ID as i already have one for each app anyway but i have no idea on how to dynamically create an array with the name of another variable. I looked at using a dictionary however i have more than two variables and no idea how to make it work with that.

Basically i need a way of storing all this information for a potentially infinite amount of applications without using a database.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
crackruckles
  • 336
  • 2
  • 5
  • 21
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 17 '13 at 13:02

2 Answers2

3

It would be useful to change your XML structure to a tree, like this:

<?xml version="1.0" encoding="utf-8"?>
<Apps>
  <App Name="First Application">
    <FileName>1.exe</FileName>
    <FilePath>C:\</FilePath>
    <Third_Parameter>etc</Third_Parameter>
    <Forth_Parameter>etc</Forth_Parameter>
  </App>
  <App Name="Application 2">
    <FilePath></FilePath>
    <Third_Parameter>etc</Third_Parameter>
    <Forth_Parameter>etc</Forth_Parameter>
  </App>
</Apps>

Then you could have this class:

Class App
  Public FileName As String
  Public FilePath As String
  Public Third_Parameter As String
  Public Forth_Parameter As String
  Public AppName As String
End Class

And a dictionary of (String,App) - assuming you would index by name.

You could populate it like this (where xml of type XDocument):

Dim dict As New Dictionary(Of String, App)
For Each elem As XElement In xml.Elements()
  Dim app As New App 'each element would be App
  With app
    .AppName = elem.Attribute("Name").Value
    .FileName = elem.Element("FileName").Value
    .FilePath = elem.Element("FilePath").Value
    .Third_Parameter = elem.Element("Third_Parameter").Value
    .Forth_Parameter = elem.Element("Forth_Parameter").Value
  End With
  dict.Add(app.AppName, app)
Next

If you want less code, consider looking into XML serialization. Some examples I found by googling:

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • I like your method however i am running into an issue with the For Each elem As XElement In xml.Elements() line. I cant seem to get it to work, it may just be that its late here and im tired but no matter what i try i just cant get it to like that line. – crackruckles Feb 17 '13 at 13:51
1

are you just concerned with storing this data while the application is running I would use a datatable

Dim x As New DataTable
        x.ReadXml("c:\pathtoxml.xml")
        x.AcceptChanges()
China Syndrome
  • 953
  • 12
  • 24
  • Just temporary storage as that application is designed to be a one time use kind of thing, hence the dynamism. The application reads from the XML document to create its UI and determine what it needs to do, so the application from a users point of view is a one time thing however from my point of view it will save me heaps of time re-writing the same application over and over again with just a few minor changes. – crackruckles Feb 17 '13 at 13:55