Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - LINQ to XML Programming Using Visual Basic...
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? 
VISUAL BASIC.NET

LINQ to XML Programming Using Visual Basic.NET 2008
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2008-12-03

    Table of Contents:
  • LINQ to XML Programming Using Visual Basic.NET 2008
  • Loading/Searching XML document with LINQ to XML using Visual Basic 2008
  • Adding, Updating and Deleting from an XML document using LINQ to XML
  • Developing a complete form to work with LINQ to XML using Visual Basic 2008
  • Developing a complete form to work with LINQ to XML using Visual Basic 2008: continued
  • How to use XPath expressions in LINQ to XML

  • 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


    LINQ to XML Programming Using Visual Basic.NET 2008 - Developing a complete form to work with LINQ to XML using Visual Basic 2008


    (Page 4 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.Linq


    Public Class Form1


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



    Dim doc As XDocument


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

    RefreshXDoc()

    End Sub



    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

    Dim qList = From xe In doc.Descendants.Elements("Employee") _

    Select New With { _

    .ID = xe.Attribute("ID").Value, _

    .Empno = xe.Element("Empno").Value, _

    .Ename = xe.Element("Ename").Value, _

    .Sal = xe.Element("Sal").Value, _

    .Deptno = xe.Element("Deptno").Value _

    }

    Me.DataGridView1.DataSource = qList.ToList

    End Sub


    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    Dim qEmp = (From xe In doc.Descendants.Elements("Employee") _

    Where xe.<Empno>.Value = Me.txtEmpno.Text _

    Select New With { _

    .ID = xe.@ID, _

    .Empno = xe.<Empno>.Value, _

    .Ename = xe.<Ename>.Value, _

    .Sal = xe.<Sal>.Value, _

    .Deptno = xe.<Deptno>.Value _

    }).FirstOrDefault

    If qEmp Is Nothing Then

    MessageBox.Show("Not found")

    Exit Sub

    End If


    Me.txtEname.Text = qEmp.Ename

    Me.txtSal.Text = qEmp.Sal

    Me.txtDeptno.Text = qEmp.Deptno

    Me.txtID.Text = qEmp.ID

    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

    Dim qTargetDept As XElement = doc.Descendants.Elements("Department") _

    .Where(Function(xe) xe.<Deptno>.Value = Me.txtDeptno.Text) _

    .FirstOrDefault

    If qTargetDept Is Nothing Then

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

    Exit Sub

    End If


    Dim oEmp As New XElement("Employee")

    oEmp.Add(New XAttribute("ID", Me.txtID.Text))

    oEmp.Add(New XElement("Empno", Me.txtEmpno.Text))

    oEmp.Add(New XElement("Ename", Me.txtEname.Text))

    oEmp.Add(New XElement("Sal", Me.txtSal.Text))

    oEmp.Add(New XElement("Deptno", Me.txtDeptno.Text))


    qTargetDept.Element("EmployeeInfo").Add(oEmp)

    SaveXMLDoc()


    MessageBox.Show("Added Successfully!")


    End Sub


    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hai,This article walks you through manipulating XML documents using LINQ. It...
       · I have test this application it work very good, I have try to add more form that...
       · Thank you for this great introduction to LINQ with VB!
     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek