Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

email article
printer friendly
more resources

Track Changes to an XML Doc
Leverage events in .NET with the Document Object Model (DOM).
by Dan Wahlin

Posted October 23, 2002

Events play a crucial role in .NET. They're used throughout the framework to provide notification to components about different activities that are occurring. For example, clicking on a Button control in an ASP.NET or Windows Forms application fires a click event that can be captured and handled. Although an end user can cause an event to be raised, in many cases events are raised by the .NET application itself. I'll demonstrate this concept through explaining how you can leverage events in .NET with the Document Object Model (DOM).

In several previous articles (see Resources), I discussed different aspects of the DOM and how you can use it to manipulate XML documents. By using the DOM, you can perform a variety of operations including inserting, deleting, modifying, and moving nodes around. What if you'd also like to track changes made within an XML document so that you have a change log to fall back on? One way of handling this would be to write your own architecture that automatically adds any changes to an XML log file. This could certainly work, but it would add extra development time and more than likely involve adding code to your DOM applications to handle the logging functionality.

Using .NET's built-in support for DOM events gives you a better alternative for tracking changes. Classes such as XmlDocument already have built-in support for events, allowing you to track changes to an XML document easily without writing a lot of custom code. For more information, read the .NET SDK's description of the different events exposed by the XmlDocument class (see Table 1).

You can track changes made to an XML document through the DOM simply by using a delegate to hook an XmlDocument's events to event handler methods. The type of delegate you must use named XmlNodeChangedEventHandler. Its signature looks like this:

public delegate void XmlNodeChangedEventHandler(
   object sender,
   XmlNodeChangedEventArgs e
);

If you're new to events in .NET, delegates allow events to be "hooked" up with event handler methods. When using XmlNodeChangedEventHandler, the event handler method (the method that is called when the event is raised) must accept as arguments an object type as well as an XmlNodeChangedEventArgs type. You'll see an example of this later in the article.

Use the XmlNodeChangedEventHandler delegate to allow handling of the NodeChanged, NodeInserted, and NodeRemoved events by defining what event handler methods will be called (see Listing 1). Looking at this code, you'll notice that the += syntax is used to "hook" the delegate to the event (C# only; VB.NET would use the AddHandler keyword).



Back to top










Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home