Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - 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


    (Page 5 of 6 )


    To demonstrate all of the previous concepts, I designed a Windows form for CRUD operations as shown in Fig 1. The following is the code developed for the form:


    Imports System.Xml


    Public Class Form1


    Const XMLDOCFILEPATH As String = "....MyData.xml"


    Dim doc As New XmlDocument


    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.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


    Me.txtEname.Text = nEmp.SelectSingleNode("Ename").InnerText

    Me.txtSal.Text = nEmp.SelectSingleNode("Sal").InnerText

    Me.txtDeptno.Text = nEmp.SelectSingleNode("Deptno").InnerText

    Me.txtID.Text = nEmp.GetAttribute("ID")

    End Sub


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    RefreshXMLDoc()

    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    For Each c As Control In Me.Controls

    If TypeOf c Is TextBox Then

    CType(c, TextBox).Text = ""

    End If

    Next

    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    'go to the EmployeeInfo node of provided department

    Dim nEmpInfo As XmlNode = doc.SelectSingleNode(String.Format("//Department[Deptno='{0}']/EmployeeInfo", Me.txtDeptno.Text))

    If nEmpInfo Is Nothing Then

    MessageBox.Show("Department/EmployeeInfo not found")

    Exit Sub

    End If


    '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


    nEmpInfo.AppendChild(nEmployee)

    SaveXMLDoc()

    MessageBox.Show("Added Successfully!")


    End Sub


    Private Sub SaveXMLDoc()

    doc.Save(XMLDOCFILEPATH)

    RefreshXMLDoc()

    btnClear_Click(Nothing, Nothing)

    End Sub


    Private Sub RefreshXMLDoc()

    doc = New XmlDocument

    doc.Load(XMLDOCFILEPATH)

    btnRefresh_Click(Nothing, Nothing)

    End Sub


    More Windows Scripting Articles
    More By Jagadish Chaterjee


       · 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 3 Hosted by Hostway
    Stay green...Green IT