Check the specific events
using System;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
XElement root = new XElement("Root",
new XElement("Total", 0),
new XElement("Items")
);
XElement total = root.Element("Total");
XElement items = root.Element("Items");
items.Changed += (object sender, XObjectChangeEventArgs cea) =>
{
switch (cea.ObjectChange)
{
case XObjectChange.Add:
if (sender is XElement)
total.Value = ((int)total + (int)(XElement)sender).ToString();
if (sender is XText)
total.Value = ((int)total + (int)((XText)sender).Parent).ToString();
break;
case XObjectChange.Remove:
if (sender is XElement)
total.Value = ((int)total - (int)(XElement)sender).ToString();
if (sender is XText)
total.Value = ((int)total - Int32.Parse(((XText)sender).Value)).ToString();
break;
}
Console.WriteLine("Changed {0} {1}", sender.GetType().ToString(), cea.ObjectChange.ToString());
};
}
}
Related examples in the same category