Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET File I/O and Registry (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/)
-   -   XML files (http://www.xtremevbtalk.com/-net-file-i-o-and-registry/326480-xml-files.html)

kyleh31 10-05-2013 08:02 AM

XML files
 
So I asked for some help on the microsoft forums and they had no clue why it was not working so I decided to post it here. This code is just trying to get an xml file to work and saves some variables into it. Want to get it working in a blank project before implementing it into my actual project.
Code:

Option Strict On
Option Explicit On

Imports System.IO.Path
Imports System.Environment

Public Class Form1

    Private Class Settings
        Public TreeCount As Long
        Public ElfCount As Long
        Public YetiCount As Long
        Public GeneratorCount As Long
        Public FactoryCount As Long
        Public IslandCount As Long
        Public PlanetCount As Long
        Public TreeCost As Long
        Public ElfCost As Long
        Public YetiCost As Long
        Public GeneratorCost As Long
        Public EneratorCost As Long
        Public FactoryCost As Long
        Public IslandCost As Long
        Public PlanetCost As Long
    End Class

    Private programDataFolderPath As String = _
        Combine(GetFolderPath(SpecialFolder.ApplicationData), _
                My.Application.Info.AssemblyName)

    Private settingsFilePath As String = Combine(programDataFolderPath, "Settings.xml")
    Private userSettings As New Settings



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load

        If Not My.Computer.FileSystem.DirectoryExists(programDataFolderPath) Then
            My.Computer.FileSystem.CreateDirectory(programDataFolderPath)
        End If

        ' I'm going to "pre-load" some just to test this, then
        ' I'll comment it out so that I can show you the result.

        'With userSettings
        '    .TreeCount = 0
        '    .ElfCount = 0
        '    .YetiCount = 0
        '    .GeneratorCount = 0
        '    .FactoryCount = 0
        '    .IslandCount = 0
        '    .PlanetCount = 0
        '    .TreeCost = 10
        '    .ElfCost = 15
        '    .YetiCost = 18000
        '    .GeneratorCost = 620000
        '    .FactoryCost = 7800000
        '    .IslandCost = 150000000
        '    .PlanetCost = 4500000000
        'End With

        'WriteSettings()
        ReadSettings()

    End Sub



    Private Sub Form1_FormClosing(ByVal sender As Object, _
                                ByVal e As System.Windows.Forms.FormClosingEventArgs) _
                                Handles Me.FormClosing

        If e.CloseReason = CloseReason.UserClosing Then
            WriteSettings()
        End If

    End Sub



    Private Sub ReadSettings()

        If My.Computer.FileSystem.FileExists(settingsFilePath) Then

            Try
                userSettings = New Settings

                Dim xInfo = XElement.Load(settingsFilePath)

                With userSettings
                    If Not Long.TryParse(xInfo...<TreeCount>.Value, .TreeCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<ElfCount>.Value, .ElfCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<YetiCount>.Value, .YetiCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<GeneratorCount>.Value, .GeneratorCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<FactoryCount>.Value, .FactoryCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<IslandCount>.Value, .IslandCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<PlanetCount>.Value, .PlanetCount) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<TreeCost>.Value, .TreeCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<ElfCost>.Value, .ElfCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<YetiCost>.Value, .YetiCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<GeneratorCost>.Value, .GeneratorCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<EneratorCost>.Value, .EneratorCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<FactoryCost>.Value, .FactoryCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<IslandCost>.Value, .IslandCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If

                    If Not Long.TryParse(xInfo...<PlanetCost>.Value, .PlanetCost) Then
                        Throw New Exception("Value could not be parsed into type long.")
                    End If
                End With

            Catch ex As Exception
                MessageBox.Show("An error occured attempting to read your" & vbCrLf & _
                                "settings file:" & vbCrLf & vbCrLf & ex.Message, "Program Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)

                Close()
            End Try
        End If

    End Sub



    Private Sub WriteSettings()

        If userSettings IsNot Nothing Then
            Try
                Dim settingsDoc = <Settings>
                                      <TreeCount><%= userSettings.TreeCount.ToString %></TreeCount>
                                      <ElfCount><%= userSettings.ElfCount.ToString %></ElfCount>
                                      <YetiCount><%= userSettings.YetiCount.ToString %></YetiCount>
                                      <GeneratorCount><%= userSettings.GeneratorCount.ToString %></GeneratorCount>
                                      <FactoryCount><%= userSettings.FactoryCount.ToString %></FactoryCount>
                                      <IslandCount><%= userSettings.IslandCount.ToString %></IslandCount>
                                      <PlanetCount><%= userSettings.PlanetCount.ToString %></PlanetCount>
                                      <TreeCost><%= userSettings.TreeCost.ToString %></TreeCost>
                                      <ElfCost><%= userSettings.ElfCost.ToString %></ElfCost>
                                      <YetiCost><%= userSettings.YetiCost.ToString %></YetiCost>
                                      <GeneratorCost><%= userSettings.GeneratorCost.ToString %></GeneratorCost>
                                      <EneratorCost><%= userSettings.EneratorCost.ToString %></EneratorCost>
                                      <FactoryCost><%= userSettings.FactoryCost.ToString %></FactoryCost>
                                      <IslandCost><%= userSettings.IslandCost.ToString %></IslandCost>
                                      <PlanetCost><%= userSettings.PlanetCost.ToString %></PlanetCost>
                                  </Settings>

                settingsDoc.Save(settingsFilePath)

            Catch ex As Exception
                MessageBox.Show("An error occured attempting to write your" & vbCrLf & _
                                "settings file:" & vbCrLf & vbCrLf & ex.Message, "Program Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

    End Sub
End Class

When I run the program it just creates a .xml file called Beta1.1(name of my project) with this code inside
Code:

<?xml version="1.0"?>
<doc>
<assembly>
<name>
Beta1.1
</name>
</assembly>
<members>
<member name="P:Beta1._1.My.Resources.Resources.ResourceManager">
        <summary>
  Returns the cached ResourceManager instance used by this class.
</summary>
</member><member name="P:Beta1._1.My.Resources.Resources.Culture">
        <summary>
  Overrides the current thread's CurrentUICulture property for all
  resource lookups using this strongly typed resource class.
</summary>
</member><member name="T:Beta1._1.My.Resources.Resources">
        <summary>
  A strongly-typed resource class, for looking up localized strings, etc.
</summary>
</member>
</members>
</doc>

Its getting me rather ****ed off to be honest.

Gruff 10-06-2013 04:45 AM

1 Attachment(s)
Not sure why you are going about this the hard way.
Visual Basic.NET has a save and load of variable built in.

At design-time you define your variables to save in the project properties under the settings tab. See attached pic.

At run time your code can access each variable by name.

Code:
Public Class Form1 Public TreeCount As Long Public ElfCount As Long Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load TreeCount = My.Settings.TreeCount ElfCount = My.Settings.ElfCount '... End Sub Private Sub Button1_Click(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Button1.Click My.Settings.TreeCount = TreeCount My.Settings.ElfCount = ElfCount '... My.Settings.Save() End Sub End Class

BTW The Form_Closing Event has issues of closing before your code completes. Use some other event(s) to save your settings.


All times are GMT -6. The time now is 12:43 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.