Dear all,
I am trying to write and better my skills coding some small junk of codes.
With the example codes from MSDN I have managed to write the below code in order to edit some selected node from an xml file.
But the below code gives me an error unexpectedly. :(
It says
Object Reference is Not Set to an Instance of an Object at the line highlighted below.
I think
root or
id returns null.
can anyone help me about this.
Code:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
'Try
'Send updates to xml file
Dim xDoc As XmlDocument = New XmlDocument()
Dim root As XmlNode = xDoc.DocumentElement
Dim id As XmlNode
Dim i As Integer
'root = xDoc.DocumentElement
For i = 0 To root.ChildNodes.Count - 1
id = root.ChildNodes(i).Attributes("CustomerID")
If (id.InnerText = txtUniqueID.Text) Then
xDoc.DocumentElement("CustomerID").Value = txtUniqueID.Text
xDoc.DocumentElement("CompanyName").Value = txtCoName.Text
xDoc.DocumentElement("ContactName").Value = txtContact.Text
xDoc.DocumentElement("ContactTitle").Value = txtTitle.Text
xDoc.DocumentElement("Address1").Value = txtAddress.Text
xDoc.DocumentElement("Phone1").Value = txtPhone1.Text
xDoc.DocumentElement("Phone2").Value = txtPhone2.Text
xDoc.DocumentElement("reason").Value = txtservice.Text
End If
Next i
xDoc.Save(m_connCustomers)
'Reset and begin again
LoadDataSet()
MsgBox("Record has been updated.")
'Attempt to update the datasource.
Me.LoadDataSet()
'Catch eUpdate As System.Exception
' 'Add your error handling code here.
' 'Display error message, if any.
' System.Windows.Forms.MessageBox.Show(eUpdate.Message)
'End Try
Me.dsCust_PositionChanged()
End Sub
Best,