EDN Admin
Well-known member
I have a windows form w/ 10 text boxes. I found this VB code to update an Xml file with the text box contents. Im still trying to test/understand it but receive the following error:
System.InvalidOperationException {"There is an error in XML document (2, 2)."}
for this line of code:
Deserialize the XML into our container object
ExistingData = Serializer.Deserialize(OpenStream)
Any help would be much appreciated.
Regards.
--------------- Complete section of code shown below --------------------------
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Create the ListOf(ListOf(String))
This is a simple container for a collection of strings that supports serialization
Dim ExistingData As New List(Of List(Of String))
Create the XML serialization object
Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(List(Of List(Of String))))
If we already have a data file, load its contents so that previously saved data is not overwritten
If System.IO.File.Exists(Application.StartupPath & "sContacts.xml") Then
Create a FileStream to read the XML text file into
Dim OpenStream As System.IO.FileStream = System.IO.File.Open(Application.StartupPath & "sContacts.xml", IO.FileMode.Open)
Deserialize the XML into our container object
ExistingData = Serializer.Deserialize(OpenStream)
Close the stream
OpenStream.Close()
End If
Now create a new ListOf(String) object to hold the new input data
Dim NewData As New List(Of String)
Add the data to the new list
NewData.Add(Sbox1.Text)
NewData.Add(Sbox2.Text)
NewData.Add(Sbox3.Text)
NewData.Add(Sbox4.Text)
NewData.Add(Sbox5.Text)
NewData.Add(Sbox6.Text)
NewData.Add(Sbox7.Text)
NewData.Add(Sbox8.Text)
NewData.Add(Sbox9.Text)
NewData.Add(Sbox10.Text)
Add the new list to the existing string list container
ExistingData.Add(NewData)
Save the updated container back to the XML file on disk
Create a file stream as we did above - note the create option rather than open
Dim SaveStream As System.IO.FileStream = System.IO.File.Open(Application.StartupPath & "sContacts.xml", IO.FileMode.Create)
Serialize the container object to XML
Serializer.Serialize(SaveStream, ExistingData)
Close the stream
SaveStream.Close()
Clear the text boxes to show that the save is complete and to allow a new entry
Sbox1.Clear()
End Sub
View the full article
System.InvalidOperationException {"There is an error in XML document (2, 2)."}
for this line of code:
Deserialize the XML into our container object
ExistingData = Serializer.Deserialize(OpenStream)
Any help would be much appreciated.
Regards.
--------------- Complete section of code shown below --------------------------
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Create the ListOf(ListOf(String))
This is a simple container for a collection of strings that supports serialization
Dim ExistingData As New List(Of List(Of String))
Create the XML serialization object
Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(List(Of List(Of String))))
If we already have a data file, load its contents so that previously saved data is not overwritten
If System.IO.File.Exists(Application.StartupPath & "sContacts.xml") Then
Create a FileStream to read the XML text file into
Dim OpenStream As System.IO.FileStream = System.IO.File.Open(Application.StartupPath & "sContacts.xml", IO.FileMode.Open)
Deserialize the XML into our container object
ExistingData = Serializer.Deserialize(OpenStream)
Close the stream
OpenStream.Close()
End If
Now create a new ListOf(String) object to hold the new input data
Dim NewData As New List(Of String)
Add the data to the new list
NewData.Add(Sbox1.Text)
NewData.Add(Sbox2.Text)
NewData.Add(Sbox3.Text)
NewData.Add(Sbox4.Text)
NewData.Add(Sbox5.Text)
NewData.Add(Sbox6.Text)
NewData.Add(Sbox7.Text)
NewData.Add(Sbox8.Text)
NewData.Add(Sbox9.Text)
NewData.Add(Sbox10.Text)
Add the new list to the existing string list container
ExistingData.Add(NewData)
Save the updated container back to the XML file on disk
Create a file stream as we did above - note the create option rather than open
Dim SaveStream As System.IO.FileStream = System.IO.File.Open(Application.StartupPath & "sContacts.xml", IO.FileMode.Create)
Serialize the container object to XML
Serializer.Serialize(SaveStream, ExistingData)
Close the stream
SaveStream.Close()
Clear the text boxes to show that the save is complete and to allow a new entry
Sbox1.Clear()
End Sub
View the full article