Visual Basic 2005 XML Programming Using XML DOM - Developing a complete form to work with XML DOM using Visual Basic.NET: continued
(Page 6 of 6 )
The following is a continuation from the previous section.
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim nEmp As XmlElement = doc.SelectSingleNode(String.Format("//Employee[Empno='{0}']", Me.txtEmpno.Text))
If nEmp Is Nothing Then
MessageBox.Show("Not found")
Exit Sub
End If
If nEmp.SelectSingleNode("Deptno").InnerText.Equals(Me.txtDeptno.Text) Then
''The following would simply modify and update the record
''However, it would not detach and attach to another dept branch (if dept is different)
nEmp.SelectSingleNode("Ename").InnerText = Me.txtEname.Text
nEmp.SelectSingleNode("Sal").InnerText = Me.txtSal.Text
nEmp.SelectSingleNode("Deptno").InnerText = Me.txtDeptno.Text 'this is not necessary
nEmp.SetAttribute("ID", Me.txtID.Text)
Else
'go to the EmployeeInfo node of provided department
Dim nTargetEmpInfo As XmlNode = doc.SelectSingleNode(String.Format("//Department[Deptno='{0}']/EmployeeInfo", Me.txtDeptno.Text))
If nTargetEmpInfo Is Nothing Then
MessageBox.Show("Department/EmployeeInfo not found")
Exit Sub
End If
'remove node from current location
Dim nSourceEmpInfo As XmlElement = nEmp.SelectSingleNode("ancestor::EmployeeInfo")
nSourceEmpInfo.RemoveChild(nEmp)
'adding node to new location
'create elements of node structure
Dim nEmployee As XmlNode = doc.CreateElement("Employee")
Dim nEmpno As XmlNode = doc.CreateElement("Empno")
Dim nEname As XmlNode = doc.CreateElement("Ename")
Dim nSal As XmlNode = doc.CreateElement("Sal")
Dim nDeptno As XmlNode = doc.CreateElement("Deptno")
Dim aID As XmlAttribute = doc.CreateAttribute("ID")
'assign values to elements
nEmpno.InnerText = Me.txtEmpno.Text
nEname.InnerText = Me.txtEname.Text
nSal.InnerText = Me.txtSal.Text
nDeptno.InnerText = Me.txtDeptno.Text
aID.InnerText = Me.txtID.Text
'form the node structure
With nEmployee
.Attributes.Append(aID)
.AppendChild(nEmpno)
.AppendChild(nEname)
.AppendChild(nSal)
.AppendChild(nDeptno)
End With
nTargetEmpInfo.AppendChild(nEmployee)
End If
SaveXMLDoc()
MessageBox.Show("Updated Successfully!")
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim nEmp As XmlElement = doc.SelectSingleNode(String.Format("//Employee[Empno='{0}']", Me.txtEmpno.Text))
If nEmp Is Nothing Then
MessageBox.Show("Not found")
Exit Sub
End If
Dim nEmpInfo As XmlElement = nEmp.SelectSingleNode("ancestor::EmployeeInfo")
nEmpInfo.RemoveChild(nEmp)
SaveXMLDoc()
MessageBox.Show("Deleted Successfully!")
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
Dim nEmps As XmlNodeList = doc.SelectNodes("//Employee")
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Empno")
dt.Columns.Add("Ename")
dt.Columns.Add("Sal")
dt.Columns.Add("Deptno")
For Each nEmp As XmlElement In nEmps
Dim dr As DataRow = dt.NewRow
dr("ID") = nEmp.GetAttribute("ID")
dr("Empno") = nEmp.ChildNodes(0).InnerText
dr("Ename") = nEmp.ChildNodes(1).InnerText
dr("Sal") = nEmp.ChildNodes(2).InnerText
dr("Deptno") = nEmp.ChildNodes(3).InnerText
dt.Rows.Add(dr)
Next
Me.DataGridView1.DataSource = dt
End Sub
End Class
My next upcoming article will focus on LINQ to XML. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |