Thursday, 16 January 2014

.Net Tips : Attach and Raise events on XML document object with C#.Net and VB.Net example

You can attach events like 'onChanging' and 'onChanged' events on XML document object. These events raise when you 'ADD' or 'REMOVE' XML element from XML Document object.

First 'onChanging' event raise after that 'onChanged' event raise. In this event we get 'XObjectChangeEventArgs' class object. This object give us property like 'ObjectChange'. In this property we can know that which method is performed.

When we call 'Add' method we get 'Add' value in 'ObjectChange' property and when we call 'Remove' method we get 'Remove' value in 'ObjectChange' property.

Here is example on this.


In this example we take one sample XML data for products. Which contains various products with price. Now we attach 'onChanging' and 'onChanged' events to XML Document object. After attaching events we add one another product element. At that time those events raise after that, we remove one object at that time also those two events raise. Now we detach that events and add another product element at that time those events won't raise.

C#. Net Example :

    public void OnChanging(object sender, XObjectChangeEventArgs e)
    {
        Response.Write("<br/>");
        Response.Write("<b>OnChanging : </b>");
        Response.Write("<br/>");
        Response.Write("<b>EventType : </b>" + e.ObjectChange);
        Response.Write("<br/>");
        Response.Write("<b>Object : </b>" + ((XNode)sender).ToString(SaveOptions.None));
        Response.Write("<br/>");
    }

    public void OnChanged(object sender, XObjectChangeEventArgs e)
    {
        Response.Write("<br/>");
        Response.Write("<b>OnChanged : </b>");
        Response.Write("<br/>");
        Response.Write("<b>EventType : </b>" + e.ObjectChange);
        Response.Write("<br/>");
        Response.Write("<b>Object : </b>" + ((XNode)sender).ToString(SaveOptions.None));
        Response.Write("<br/>");
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        string objXMLDOC = "<products>" +
                        "<product price='150'>LCD</product>" +
                        "<product price='50'>Oven</product>" +
                        "<product price='50'>Keyboard</product>" +
                        "<product price='250'>Monitor</product>" +
                        "<product price='10'>Mouse</product>" +
                     "</products>";
        XDocument objProducts = XDocument.Parse(objXMLDOC);
        // Add event listners to product
        
        objProducts.Changing += new EventHandler<XObjectChangeEventArgs>(OnChanging);
        objProducts.Changed += new EventHandler<XObjectChangeEventArgs>(OnChanged);
        // Add a new item to product
        Response.Write("<br/>");
        Response.Write("Original product:" + objProducts.ToString(SaveOptions.None));

        objProducts.Root.Add(new XElement("product", new XAttribute("price", 350), "AC"));
        Response.Write("<br/>");
        Response.Write("After Add:" + objProducts.ToString(SaveOptions.None));
        objProducts.Root.Element("product").Remove();
        Response.Write("<br/>");
        Response.Write("After Remove:" + objProducts.ToString(SaveOptions.None));
        
        // Remove event listners
        objProducts.Changing -= new EventHandler<XObjectChangeEventArgs>(OnChanging);
        objProducts.Changed -= new EventHandler<XObjectChangeEventArgs>(OnChanged);

        // Add another element, events should not be raised
        objProducts.Root.Add(new XElement("item", new XAttribute("price", 350), "AC"));

    }

VB.Net Examples :

    Public Sub OnChanging(sender As Object, e As XObjectChangeEventArgs)
        Response.Write("<br/>")
        Response.Write("<b>OnChanging : </b>")
        Response.Write("<br/>")
        Response.Write("<b>EventType : </b>" & e.ObjectChange)
        Response.Write("<br/>")
        Response.Write("<b>Object : </b>" & DirectCast(sender, XNode).ToString(SaveOptions.None))
        Response.Write("<br/>")
    End Sub

    Public Sub OnChanged(sender As Object, e As XObjectChangeEventArgs)
        Response.Write("<br/>")
        Response.Write("<b>OnChanged : </b>")
        Response.Write("<br/>")
        Response.Write("<b>EventType : </b>" & e.ObjectChange)
        Response.Write("<br/>")
        Response.Write("<b>Object : </b>" & DirectCast(sender, XNode).ToString(SaveOptions.None))
        Response.Write("<br/>")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim objXMLDOC As String = "<products>" &
                                        "<product price='150'>LCD</product>" &
                                        "<product price='50'>Oven</product>" &
                                        "<product price='50'>Keyboard</product>" &
                                        "<product price='250'>Monitor</product>" &
                                        "<product price='10'>Mouse</product>" &
                                    "</products>"
        Dim objProducts As XDocument = XDocument.Parse(objXMLDOC)

        ' Add event listners to product
        AddHandler objProducts.Changing, AddressOf OnChanging
        AddHandler objProducts.Changed, AddressOf OnChanged


        Response.Write("<br/>")
        Response.Write("Original Order:" & objProducts.ToString(SaveOptions.None))

        objProducts.Root.Add(New XElement("item", New XAttribute("price", 350), "Printer"))
        Response.Write("<br/>")

        Response.Write("After Add:" & objProducts.ToString(SaveOptions.None))
        objProducts.Root.Element("item").Remove()

        Response.Write("<br/>")
        Response.Write("After Remove:" & objProducts.ToString(SaveOptions.None))


        RemoveHandler objProducts.Changing, AddressOf OnChanging
        RemoveHandler objProducts.Changed, AddressOf OnChanged

        objProducts.Root.Add(New XElement("item", New XAttribute("price", 350), "Printer"))
    End Sub

Output : 


Below are the books that you would like :

3 comments:

  1. Your information about c# is really interesting and innovative. Also I want you to share latest updates about c#. Can you update it in your website? Thanks for sharing

    ReplyDelete
    Replies
    1. Thanks for your comments.
      Yes, when ever I got interesting information I will post in my blog.

      Delete
  2. This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete