Search code examples
c#.netxmllinq-to-xmlxmldocument

Register to a changed event on construction of Xdocument or XmlDocument


is there a a way to register to the changed event of the XDocument class, or the NodeChanged event of the XmlDocument class while the class is being constructed ?

the following code did not work for me (the handler was not called) :

string fileName = "MyFile.xml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.NodeChanged += new XmlNodeChangedEventHandler(xmlDocument_NodeChanged);
xmlDocument.Load(fileName);

 void xmlDocument_NodeChanged(object sender, XmlNodeChangedEventArgs e)
        {
            throw new NotImplementedException();
        }

Solution

  • Form MSDN,

    XObject : Events are raised only when an XML tree is modified, not when it is constructed.

    I couldn't find an equally pertinent statement for XmlDocument but it seems reasonable not to raise NodeChanged on creation. You might try NodeInserted.

    Update:

    I just tried, NodeInserting and NodesInserted are firing during Load().