Listen to Add, Remove events
Imports System
Imports System.Xml
Imports System.Xml.Schema
Module Module1
Private total As XElement = Nothing
Private WithEvents items As XElement = Nothing
Private root As XElement = _
<Root>
<Total>0</Total>
<Items></Items>
</Root>
Sub Main()
total = root.<Total>(0)
items = root.<Items>(0)
items.SetElementValue("Item1", 25)
items.SetElementValue("Item1", Nothing)
Console.WriteLine("Total:{0}", CInt(total))
Console.WriteLine(root)
End Sub
Private Sub XObjectChanged( _
ByVal sender As Object, _
ByVal cea As XObjectChangeEventArgs) _
Handles items.Changed
Select Case cea.ObjectChange
Case XObjectChange.Add
If sender.GetType() Is GetType(XElement) Then
total.Value = CStr(CInt(total.Value) + _
CInt((DirectCast(sender, XElement)).Value))
End If
If sender.GetType() Is GetType(XText) Then
total.Value = CStr(CInt(total.Value) + _
CInt((DirectCast(sender, XText)).Value))
End If
Case XObjectChange.Remove
If sender.GetType() Is GetType(XElement) Then
total.Value = CStr(CInt(total.Value) - _
CInt((DirectCast(sender, XElement)).Value))
End If
If sender.GetType() Is GetType(XText) Then
total.Value = CStr(CInt(total.Value) - _
CInt((DirectCast(sender, XText)).Value))
End If
End Select
Console.WriteLine("Changed {0} {1}", _
sender.GetType().ToString(), _
cea.ObjectChange.ToString())
End Sub
End Module
Related examples in the same category