XML files
XML files
XML files
XML files
XML files
XML files XML files XML files XML files XML files XML files XML files XML files
XML files XML files
XML files
Go Back  Xtreme Visual Basic Talk > > > XML files


Reply
 
Thread Tools Display Modes
  #1  
Old 10-05-2013, 08: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
  #2  
Old 10-06-2013, 04:45 AM
Gruff's Avatar
GruffXML files Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

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.
Attached Images
File Type: png Settings Tab.PNG (62.3 KB, 7 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 10-06-2013 at 05:48 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
XML files
XML files
XML files XML files
XML files
XML files
XML files XML files XML files XML files XML files XML files XML files
XML files
XML files
 
XML files
XML files
 
-->