Windows Scripting
  Home arrow Windows Scripting arrow Page 6 - Visual Basic 2005 XML Programming Using XM...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Visual Basic 2005 XML Programming Using XML DOM
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-11-26

    Table of Contents:
  • Visual Basic 2005 XML Programming Using XML DOM
  • Sample XML document: source
  • Loading/Searching an XML document with XML DOM using Visual Basic.NET
  • Adding, Updating and Deleting From the XML Document with XML DOM Using Visual Basic.NET
  • Developing a complete form to work with XML DOM using Visual Basic.NET
  • Developing a complete form to work with XML DOM using Visual Basic.NET: continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · Hai,This article introduces CRUD operations to XML document using Visual Basic...
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT