In the code below I raise an exception when a non-integer value is set
as
the new orderID on an attribute.
It correctly determines this and raises and exception. But the Dom doc
is
supposed to go back to it's prior state,
but it doesn't and the attrib value still gets updated (and the error
also
gets displayed!). So what am i missing?

...
 public void Run(String args)
 {

  // Create and load the XML document.
  Console.WriteLine ("Loading file {0} ...", args);
  XmlDocument doc = new XmlDocument();
  doc.Load (args);

  //Create the event handlers.
  doc.NodeChanged += new
XmlNodeChangedEventHandler(this.MyNodeChangedEvent);

  try
  {
   // Change the book price.
   doc.DocumentElement.Attributes.GetNamedItem("orderID").Value =
"1244C";
  }
  catch (Exception ex)
  {
   Console.WriteLine(ex.Message);
  }

  Console.WriteLine("\r\nDisplay the modified XML...");
  Console.WriteLine(doc.OuterXml);
  Console.ReadLine();
 }

 // Handle the NodeChanged event.
 public void MyNodeChangedEvent(Object src, XmlNodeChangedEventArgs
args)
 {
  Console.WriteLine("OrderID being updated with value " +
args.Node.Value);

  try
  {
   int newid = Convert.ToInt32(args.Node.Value);
   Console.WriteLine(" with value  {0}", newid.ToString());
  }
  catch
  {
   throw new Exception(args.Node.Value + " must be an integer value");
  }
 }
...

Output.................

Loading file ..\..\nodeChange.xml ...
OrderID being updated with value 1244C
1244C must be an integer value

Display the modified XML...
<?xml version="1.0" encoding="utf-8"?><PurchaseOrder orderID="1244C" />

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to