using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using LinqTest.classes; using System.Xml.Linq; using System.Xml; using System.Collections; namespace LinqTest { partial class Program { static void Main(string[] args) { // Create a sample XML XElement ClassicIcecreams = new XElement("Icecreams", new XElement("Icecream", new XElement("Name", "Chocolate Fudge Icecream"), new XElement(@"Ingredients", "cream, milk, sugar, corn syrup, cellulose gum..."), new XElement("Cholesterol", "50mg")) ); //添加事件处理程序 当修改前触发 ClassicIcecreams.Changing += new EventHandler<XObjectChangeEventArgs>(ClassicIcecreams_Changing); // Create a new XML element XElement NewIcecream = new XElement("Icecream1", new XElement("Name", "Vanilla Icecream"), new XElement("Ingredients", "vanilla extract, guar gum, cream, nonfat milk, sugar, locust bean gum, carrageenan, annatto color..."), new XElement("Cholesterol", "65mg") ); ClassicIcecreams.Add(NewIcecream); Console.ReadLine(); } static void ClassicIcecreams_Changing(object sender, XObjectChangeEventArgs e) { Console.WriteLine("是否要继续操作(Y/N)?"); if (Console.ReadLine().ToString().ToLower() == "y") { //此处添加操作逻辑 XElement eleSend = (XElement)sender; //请添加System.Windows.Forms的dll引用 System.Windows.Forms.MessageBox.Show("XML is Changing " + " /n " + " Sender: " + eleSend.Name.LocalName + " Operation: " + e.ObjectChange.ToString(), "Changing Event"); Console.WriteLine("操作完毕"); } else { Console.WriteLine("操作取消"); } } }//end class }//end namespace