Thread: XML files
View Single Post
 
Old 10-05-2013, 07:02 AM
kyleh31 kyleh31 is offline
Newcomer
 
Join Date: Oct 2013
Posts: 1
Default 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.
Reply With Quote