Results 1 to 2 of 2

Thread: XML File (Save, Open and Append)

  1. #1

    Thread Starter
    Member G33kman's Avatar
    Join Date
    Nov 2009
    Location
    Mankato, MN
    Posts
    40

    XML File (Save, Open and Append)

    Ok I have been reading tutorials and searching Google but it just seems like there's 10 different ways to save and open xml documents and I'm getting confused on how to apply this to what I'm trying to do.

    The Controls that I have on the form:

    1 Combo box (cbxRep)
    2 Check Boxes (chkLooseFill and chkRetroFoam)
    1 READ ONLY text box (txtEstNumber)
    1 Month Calendar (Calendar)
    3 Textboxes (txtFirstName, txtLastName and txtComments)
    3 Buttons (btnOpen, btnSave and btnCancel)
    1 savefiledialog (sfdBox)
    1 openfiledialog (ofdBox)

    What my form looks like:

    Name:  reichel-test-log.jpg
Views: 318
Size:  96.9 KB

    What I'm trying to achieve:

    I want to be able to do three different things here.

    1: I want to be able to save the information (I already partially have this working but I'm not sure if this is the best way to achieve the results) to an XML file.

    2: have the program append any information if it already exists (update customer information)

    3: When the open button is clicked I need the ofdBox to open and when the file is clicked I need a window to open listing out all the information from the file separated into each customers information so that they can select the right customer estimate that they want.

    Sorry if this seems confusing or like a lot I try to be thorough when I post something to clear up any confusion people may have...
    If it needs to be broken up or something let me know. I'm trying to figure this out but it's proving to be more than I thought it would be...

    Code I've used so far:

    ' Checks to see if the folder and file exist if not creates them.

    Code:
    'UNIVERSAL VARIABLES
        'Get current year and month
        Dim thisYear As Integer = Now.Year
        Dim thisMonth As Integer = Now.Month
    
    
        'NEW SUB
        Sub New()
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            txtEstNumber.Text = 0
        End Sub
    
        'MAINWINDOW FORM LOAD
        Private Sub testLog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            If Dir(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Reichel Log Files\" & thisYear & "\", FileAttribute.Directory) = "" Then
                MkDir(Path:=My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Reichel Log Files\" & thisYear)
                My.Computer.FileSystem.CopyDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Reichel Log Files\" & "Default\*", My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Reichel Log Files\" & thisYear & "\", True)
            Else
                Exit Sub
            End If
    
        End Sub
    ' Writes out the information to an XML File (but overwrites the file everytime.
    Code:
    'Save Button
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
            Dim writer As New XmlTextWriter(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Reichel Log Files\" & thisYear & "\" & thisMonth & "\Reichel_Test_Log.rei", System.Text.Encoding.UTF8)
            writer.WriteStartDocument(True)
            writer.Formatting = Formatting.Indented
            writer.Indentation = 2
            writer.WriteStartElement("Customer Information")
            createNode(txtFirstName.Text, txtLastName.Text, txtComments.Text, writer)
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Close()
    
        End Sub
    
        Private Sub createNode(ByVal txtFirstName As String, ByVal txtLastName As String, ByVal txtComments As String, ByVal writer As XmlTextWriter)
    
            writer.WriteStartElement("Estimate Number")
            writer.WriteString(txtEstNumber.Text)
            writer.WriteEndElement()
            writer.WriteStartElement("Date Created")
            writer.WriteStartElement("Date")
            writer.WriteString(Now.Date)
            writer.WriteEndElement()
            writer.WriteStartElement("Time")
            writer.WriteString(TimeOfDay)
            writer.WriteEndElement()
            writer.WriteEndElement()
            writer.WriteStartElement("Sales Rep")
            writer.WriteString(cbxRep.SelectedItem)
            writer.WriteEndElement()
            writer.WriteStartElement("JobsChecked")
            writer.WriteStartElement("Loosefill")
            writer.WriteString(chkLooseFill.CheckState)
            writer.WriteEndElement()
            writer.WriteStartElement("RetroFoam")
            writer.WriteString(chkRetroFoam.CheckState)
            writer.WriteEndElement()
            writer.WriteEndElement()
            writer.WriteStartElement("FirstName")
            writer.WriteString(txtFirstName)
            writer.WriteEndElement()
            writer.WriteStartElement("LastName")
            writer.WriteString(txtLastName)
            writer.WriteEndElement()
            writer.WriteStartElement("Comments")
            writer.WriteString(txtComments)
            writer.WriteEndElement()
    
        End Sub

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: XML File (Save, Open and Append)

    There are many different ways wit xml.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private ReadOnly m_fileName As String = "c:\Settings.xml"
    4.  
    5.     Private Sub SaveButton_Click(ByVal sender As System.Object,
    6.                                  ByVal e As System.EventArgs) Handles SaveButton.Click
    7.  
    8.         Dim xDoc As New XDocument
    9.  
    10.         If IO.File.Exists(m_fileName) Then
    11.             xDoc = XDocument.Load(m_fileName)
    12.         Else
    13.             xDoc = XDocument.Parse( _
    14.             "<?xml version=""1.0"" encoding=""utf-8""?><Settings></Settings>")
    15.         End If
    16.         Dim config = xDoc.<Settings>(0)
    17.  
    18.         config.Add(<Setting>
    19.                        <FirstName><%= Me.FirstNameTextBox.Text %></FirstName>
    20.                        <Lastname><%= Me.LastNameTextBox.Text %></Lastname>
    21.                        <SomeCheckBox><%= Me.SomeCheckBox.Checked %></SomeCheckBox>
    22.                    </Setting>)
    23.  
    24.         config.Save(m_fileName)
    25.     End Sub
    26.  
    27. End Class
    My Github - 1d3nt

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width