DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   Writing to an XML without overwriting data (http://www.daniweb.com/forums/thread201590.html)

Tamir09 Jul 4th, 2009 2:32 am
Writing to an XML without overwriting data
 
Ive managed so far with this code that amends data to an xml form, but I need to figure out how to remove the bit of code that amends data rather than overwriting it. Ive messed around with the code to see if I could manipulate it myself to get be able to overwrite data, but I havent been able to, so I ask for help.

ElseIf ComboBox1.Text = "Turbine Data" Then
            'start of first combo box code
            Dim filepath As String = My.Application.Info.DirectoryPath & turbdatxml

            If Dir(filepath) <> "" Then

                If (TextBox1.Text.Equals("") Or TextBox2.Text.Equals("") Or TextBox3.Text.Equals("") Or TextBox4.Text.Equals("")) Then

                    MsgBox(val_txt, , inval_title)

                Else
                    Dim ExistingData As New MyDatum

                    Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(MyDatum))

                    If (File.Exists(filepath)) Then '& Not EOF(1)

                        'Need to find out how to check End of File, and skip this bit of code if file is empty

                        Dim OpenStream As System.IO.FileStream = System.IO.File.Open(filepath, IO.FileMode.Open)

                        'Protects existing data, to make a list, rather than overwriting data.
                        ExistingData = Serializer.Deserialize(OpenStream)

                        OpenStream.Close()
                    Else

                        MsgBox(cre05, , err05)

                    End If

                    Dim NewData As New MyData

                    TextBox1.Focus()
                    NewData.DateStamp = CDate(Date.Today()) 'DateValue(Now)
                    NewData.TimeStamp = CDate(TimeString())
                    'find out how to display the date and time seperate, not as a stamp
                    NewData.Gap = gaptext.Text
                    NewData.Diameter = diatext.Text
                    NewData.R2 = TextBox1.Text
                    NewData.VentDiamater = TextBox2.Text
                    NewData.R1 = TextBox3.Text
                    NewData.Height = TextBox4.Text

                    ExistingData.Add(NewData)

                    Dim SaveStream As System.IO.FileStream = System.IO.File.Open(filepath, IO.FileMode.Open)
                    ' IO.FileMode.Create or .Open

                    Serializer.Serialize(SaveStream, ExistingData)

                    SaveStream.Close()

                    MsgBox("Data Saved to " & filepath, , _save)

                End If

            Else

                MsgBox(cre05, , err05)

                Dim ExistingData As New MyDatum
                Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(MyDatum))
                Dim NewData As New MyData
                Dim SaveStream As System.IO.FileStream = System.IO.File.Open(filepath, IO.FileMode.Create)

                TextBox1.Focus()
                NewData.DateStamp = CDate(Date.Today()) 'DateValue(Now)
                NewData.TimeStamp = CDate(TimeString())
                NewData.Gap = gaptext.Text
                NewData.Diameter = diatext.Text
                NewData.R2 = TextBox1.Text
                NewData.VentDiamater = TextBox2.Text
                NewData.R1 = TextBox3.Text
                NewData.Height = TextBox4.Text

                Serializer.Serialize(SaveStream, ExistingData)
                SaveStream.Close()
            End If
            'End of first combo box code

I need to keep with this original code as far as the serializing of the data is concerned, I just need to remove the 'ExistingData' component from the code, and still be able to write data to the xml.

adatapost Jul 5th, 2009 12:52 am
Re: Writing to an XML without overwriting data
 
Tamir09,
Quote:

I need to keep with this original code as far as the serializing of the data is concerned, I just need to remove the 'ExistingData' component from the code, and still be able to write data to the xml.
Do you want to modify the XML document or your source code?

Tamir09 Jul 5th, 2009 5:22 pm
Re: Writing to an XML without overwriting data
 
I would prefer to modify my source code.

Tamir09 Jul 7th, 2009 9:29 pm
Re: Writing to an XML without overwriting data
 
Any help would be appreciated...

adatapost Jul 7th, 2009 9:35 pm
Re: Writing to an XML without overwriting data
 
Use DOM.

Tamir09 Jul 7th, 2009 11:47 pm
Re: Writing to an XML without overwriting data
 
Quote:

Originally Posted by adatapost (Post 911289)
Use DOM.

What is DOM? And how will I be able to apply that to my code?

adatapost Jul 8th, 2009 1:08 am
Re: Writing to an XML without overwriting data
 
Document Object Model - a standard to work with XML document. See the System.XML namespace.

Dim doc as New  System.Xml.XmlDocument()
doc.Load("file.xml") 'Load an XML document
...
...
doc.Save("file.xml") 'Save the document

Tamir09 Jul 8th, 2009 4:00 am
Re: Writing to an XML without overwriting data
 
Thanks for the suggestions, but my question is how I can change my current code to allow for overwriting my xml data?


All times are GMT -4. The time now is 11:33 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC