Check the specific events : XObject « XML LINQ « C# / C Sharp






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

1.Adds an object to the annotation list of this XObject.
2.Gets the first annotation object of the specified type from this XObject.
3.Get the first annotation object of the specified type from this XObject.
4.Gets a collection of annotations of the specified type for this XObject.
5.Gets the base URI for this XObject.
6.Raise event when this XObject or any of its descendants are changing
7.Raise event when this XObject or any of its descendants are changed
8.Gets the node type for this XObject.