Hi Daniel,
Thanks for posting in the community.
First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to track a property's
change of one object and log it onto one xml file.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.
I think you can try to serialize the Name as an attribute also declare the
timestamp to know when the weight change.
The Log File will look like below.
<?xml version="1.0" encoding="utf-8" ?>
- <LogFile xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"> - <Dog Name="Fido" TimeStamp="2004-02-12T16:12:59.7086582+08:00">
<Weight>20</Weight>
</Dog>
</LogFile>
[The class1.vb file used to (de)serialize the file]
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
Imports System.IO
<System.Xml.Serialization.XmlRootAttribute([Namespace]:="",
IsNullable:=False)> _
Public Class LogFile
<System.Xml.Serialization.XmlElementAttribute("Dog",
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Items() As Dog
End Class
Public Class Dog
Public Event Weight_Changed()
'
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSch
emaForm.Unqualified)> _
Private _Weight As Byte
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSch
emaForm.Unqualified)> _
Public Property Weight() As Byte
Get
Return _Weight
End Get
Set(ByVal Value As Byte)
If Not Value = _Weight Then
_Weight = Value
RaiseEvent Weight_Changed()
End If
End Set
End Property
' <System.Xml.Serialization.XmlAttributeAttribute()> _
Private _Name As String
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Private _TimeStamp As DateTime
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property TimeStamp() As DateTime
Get
Return _TimeStamp
End Get
Set(ByVal Value As DateTime)
_TimeStamp = Value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal nm As String)
_Name = nm
End Sub
End Class
[The Main file]
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
Imports System.IO
Imports System.Text
Module Module1
Dim ds As DataSet
Dim lf As LogFile
Sub Main()
' At the first time create the file
'CreateLogFile()
Dim dt As DateTime
'Deserialize an object
Dim x As XmlSerializer = New XmlSerializer(GetType(LogFile))
Dim reader As TextReader = New StreamReader("C:\LogFile.xml")
lf = x.Deserialize(reader)
ds = New DataSet
ds.ReadXml("C:\LogFile.xml")
AddHandler lf.Items(0).Weight_Changed, AddressOf Dog_Weight_Changed
lf.Items(0).Weight = 20
'Change the Weight will be log to file
lf.Items(0).Weight = 30
'You also can write to a file
ds.WriteXml(Console.Out)
End Sub
Private Sub CreateLogFile()
Dim dg As Dog
Dim lb As New LogFile
dg = New Dog("Fido")
dg.TimeStamp = Now
dg.Weight = 20
lb.Items = New Dog() {dg}
Dim x As XmlSerializer = New XmlSerializer(GetType(LogFile))
Dim writer As TextWriter = New StreamWriter("C:\LogFile.xml")
x.Serialize(writer, lb)
writer.Close()
End Sub
Private Sub Dog_Weight_Changed()
Dim x As XmlSerializer = New XmlSerializer(GetType(LogFile))
'Dim writer As TextWriter = New StreamWriter("Hello.xml")
Dim sm As MemoryStream = New IO.MemoryStream
x.Serialize(sm, lf)
sm.Position = 0
Dim reader As Xml.XmlTextReader = New Xml.XmlTextReader(sm)
ds.ReadXml(reader)
sm.Close()
End Sub
End Module
Did this works for you?
If you have any concern on this issue,please post here.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.