Herfried K. Wagner [MVP] wrote:
"schneider" <ab*@123.com> schrieb:
Have an issue with a property using the DefaultValue(Tr ue) attribute.
[...]
here's the steps to reproduce the problem:
1. User UI sets the Class1.test=fal se during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=tru e during runtime.
4. I serialize Class1 to xml and the True value is NOT saved and the
False value is still there.
Step 4 is the problem, the file does not seem to reflect the current
state of the class.
\\\
Imports System.Componen tModel
.
.
.
< _
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible), _
DefaultValue(Tr ue) _
_
Public Property...
///
Does not seem to work, I include a more detailed sample (must be missing
something):
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Dim c As New Class1
c.test = False
Class1.Serializ eObjectToFile(" C:\test.txt", c)
c.test = True
Class1.Serializ eObjectToFile(" C:\test.txt", c)
Dim b As Class1
b = DirectCast(Clas s1.DeserializeO bjectFromFile(" C:\test.txt"), Class1)
Dim a As Integer
a = 1
End Sub
Imports System.Componen tModel
Imports System.IO
Public Class Class1
Private m_testValue As Boolean
<DefaultValue(T rue),
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible)> _
Public Property test() As Boolean
Get
Return m_testValue
End Get
Set(ByVal value As Boolean)
m_testValue = value
End Set
End Property
Public Shared Sub SerializeObject ToFile(ByVal fileName As String, ByVal
value As Object)
Dim writer As New StreamWriter(fi leName)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serializati on.XmlSerialize r(value.GetType )
' Serialize the object, and close the StreamWriter.
s.Serialize(wri ter, value)
Catch
Throw
Finally
writer.Close()
End Try
End Sub
Public Shared Function DeserializeObje ctFromFile(ByVa l fileName As
String) As Object
Dim fs As New IO.FileStream(f ileName, FileMode.Open)
Try
Dim w As New Xml.Serializati on.XmlSerialize r(GetType(Class 1))
Dim g As Object = w.Deserialize(f s)
Return g
Catch
Throw
Finally
fs.Close()
End Try
End Function
End Class