Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Loading/Searching an XML document with XML DOM using Visual Basic.NET


    (Page 3 of 6 )

    The starting point for learning XML programming is to load the XML document into an object. Let us look at that:


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

    Dim doc As XmlDocument

    doc = New XmlDocument

    doc.Load(XMLDOCFILEPATH)


    In the above code, I am creating a new XMLDocument object and loading XML into it. Make sure that you import "System.XML" before you work on XML-related classes.

    Once the XML is loaded into the "XMLDocument" object, we can mold it to any kind of object. For instance, we can create a strongly typed data table using the following code:


    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


    The alternative way to achieve something similar to the above is by using "Dataset.ReadXml."

    If you observe the above code snippet, I used "//Employee" to retrieve only "Employee" elements (along with their child elements). "//Employee" is an XPath expression. XPath is a simple "search" kind of technology for XML. It has its own operators and syntax. By using XPath expressions we can retrieve/find any part of the provided XML.

    Let us consider finding single employee information (using XPath) based on the employee number provided. The following is the code:


    Dim nEmp As XmlElement = doc.SelectSingleNode("//Employee[Empno='1007']")


    Once the employee is found, we can retrieve information about its child elements as follows:


    a = nEmp.SelectSingleNode("Ename").InnerText

    b = nEmp.SelectSingleNode("Sal").InnerText

    c = nEmp.SelectSingleNode("Deptno").InnerText

    d = nEmp.GetAttribute("ID")


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