Listen to Add, Remove events : XML Edit « XML LINQ « VB.Net






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

1.Remove content from XElement
2.Remove from XElement
3.Remove the 4th Employee element.
4.XElement.RemoveAll Method removes nodes and attributes from this XElement.
5.XElement.RemoveAttributes removes the attributes of this XElement.
6.XElement.SetAttributeValue sets the value of an attribute, adds an attribute, or removes an attribute.
7.XElement.SetElementValue sets the value of a child element, adds a child element, or removes a child element.
8.Replace content from XElement
9.XElement.ReplaceAll replaces child nodes and attributes with the specified content.
10.ReplaceAll with an array of XElement objects
11.XElement.ReplaceAll(Object[]) replaces child nodes and attributes with the specified content.
12.XElement.ReplaceAttributes (Object) replaces the attributes with the specified content.
13.XElement.ReplaceAttributes (Object[]) replaces attributes with the specified content.
14.ReplaceAll with a string
15.ReplaceAll with a double
16.ReplaceAll with a DateTime object
17.ReplaceAll with a string array
18.Update attribute for XElement
19.Update XElement
20.Adding XElement to XElement
21.Add 5 new green Fords to the incoming document
22.Add the new node to the bottom of the XML tree