Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant Solution For Storing XML Data

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.

like image 511
crackruckles Avatar asked Jun 28 '26 18:06

crackruckles


2 Answers

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:

  • How do I write objects for easy XML Serialization in VB.NET?
  • HOW TO: Serialize and Deserialize XML in Visual Basic .NET
  • XML Serialization - Part 1 on CodeProject
like image 116
Neolisk Avatar answered Jul 01 '26 12:07

Neolisk


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()
like image 27
China Syndrome Avatar answered Jul 01 '26 11:07

China Syndrome



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!