Using XML Collection Events
Description
The Liquid XML Runtime for .Net XMLCollectionBase fires events whenever items are added or removed from the collection. The following code, when added to the Price Enquiry Sample, shows how to catch these events and the sequence that they are fired.
private static void SimpleTestQuoteRequest() { PriceEnquirySampleLib.QuoteRequest elm = new PriceEnquirySampleLib.QuoteRequest(); // set up events elm.Item.OnCollectionAdd += new XmlCollectionBase.OnCollectionAddEvent(OnAdd); elm.Item.OnCollectionAddComplete += new XmlCollectionBase.OnCollectionAddCompleteEvent(OnAddComplete); elm.Item.OnCollectionRemove += new XmlCollectionBase.OnCollectionRemoveEvent(OnRemove); elm.Item.OnCollectionRemoveComplete += new XmlCollectionBase.OnCollectionRemoveCompleteEvent(OnRemoveComplete); elm.Item.OnCollectionChange += new XmlCollectionBase.OnCollectionChangeEvent(OnChange); // Add a new item to collection PriceEnquirySampleLib.Item i = elm.Item.Add(); // Remove the new item from collection elm.Item.Remove(0); } static void OnAdd(object o, XmlCollectionAddEventArgs args) { Console.WriteLine("OnAdd() Items: " + ((XmlCollectionBase)o).Count); } static void OnAddComplete(object o, XmlCollectionAddEventArgs args) { Console.WriteLine("OnAddComplete() Items: " + ((XmlCollectionBase)o).Count); } static void OnRemove(object o, XmlCollectionRemoveEventArgs args) { Console.WriteLine("OnRemove() Items: " + ((XmlCollectionBase)o).Count); } static void OnRemoveComplete(object o, XmlCollectionRemoveEventArgs args) { Console.WriteLine("OnRemoveComplete() Items: " + ((XmlCollectionBase)o).Count); } static void OnChange(object o, EventArgs args) { Console.WriteLine("OnChange() Items: " + ((XmlCollectionBase)o).Count); }
The Output from these event methods is:
OnAdd() Items: 0 OnAddComplete() Items: 1 OnChange() Items: 1 OnRemove() Items: 1 OnRemoveComplete() Items: 0 OnChange() Items: 0
The XmlCollectionAddEventArgs provides the property 'item' providing the newly added item and the XmlCollectionRemoveEventArgs provides the property 'index' to specify the position at which the item was removed.