Microsoft KB Archive/326599

= BUG: XmlDataDocument Does Not Fire NodeChanged Events =

Article ID: 326599

Article Last Modified on 9/24/2003

-

APPLIES TO


 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q326599



SYMPTOMS
If an XmlDataDocument object is bound to a DataSet object, and the DataSet data is changed, the XmlDataDocument does not fire NodeChanged events.



CAUSE
When the XmlDataDocument object is bound to a DataSet object, the XmlDataDocument does not contain nodes. Nodes are created when they are first accessed. Because the XmlDataDocument does not yet contain nodes, changes to the DataSet do not trigger NodeChanged events.



RESOLUTION
To work around this problem, you must access each and every node in the document tree. To do this, use the InnerText property of the XmlDataDocument object one time. When each node has been accessed (and, therefore, created) all events fire.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Projects. Click Visual Basic Projects under Project Types, and then click Windows Application under Templates. By default, Form1.vb is created. Drag a DataGrid control (DataGrid1) and a Button control (Button1) from the toolbox to Form1.  Import the following namespaces: Imports System.Xml Imports System.Data.SqlClient   Dim the following as a global variable: Dim WithEvents xmldoc As Xml.XmlDataDocument </li>  Paste the following code in the Button1_click event: Try 'Open a new connection to SQL Server database. Dim cnNwind As New SqlConnection(&quot;data source=SqlServername;integrated security=SSPI;initial catalog=Northwind;&quot;) Dim daProducts As New SqlDataAdapter(&quot;Select * from Products&quot;, cnNwind) Dim ds as DataSet 'Fill the DataSet. ds = New DataSet daProducts.Fill(ds, &quot;Products&quot;) 'Bind the XmlDataDocument to the DataSet xmldoc = New Xml.XmlDataDocument(ds)

Me.DataGrid1.DataSource = ds   Me.DataGrid1.DataMember = &quot;Products&quot; 'Workaround 'Dim dummy As String 'dummy = xmldoc.InnerText Catch ex As Exception MsgBox(ex.ToString) End Try </li>  To add the XmlDataDocument class NodeChanged event, paste the following code: Private Sub xmldoc_NodeChanged(ByVal sender As Object, ByVal e As System.Xml.XmlNodeChangedEventArgs) Handles xmldoc.NodeChanged MsgBox(&quot;xmldoc_NodeChanged event fired&quot;) End Sub </li> Compile and run the project. Notice that the DataGrid is filled with products data.</li> Change the data in the grid, and then click Next row. Notice that the XmlDataDocument does not fire the NodeChanged event.</li> To use the InnerText property of the XmlDataDocument, uncomment the two lines of code after &quot;Workaround&quot;, and then run the project.</li>  Change the data in the grid, and then click Next row. Notice the following message: xmldoc_NodeChanged event fired </li></ol>

<div class="references_section">