Click here to Skip to main content
15,893,814 members
Articles / Web Development / ASP.NET

Writing a LINQ query on an XML data file: Step by step explanation in VB.NET (VS2008)

Rate me:
Please Sign up or sign in to vote.
3.91/5 (5 votes)
2 Jun 2008CPOL2 min read 72.1K   512   21  
VB.NET LINQ query on an XML file.

Partial Class EmpDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub

    Private Sub BindData()
        Dim pk As Integer
        pk = CInt(Session("EMPID"))

        Dim xmlDS = XElement.Load(Request.PhysicalApplicationPath & "/App_Data/Employee.xml")
        Dim query = From row In xmlDS.Elements Where (CType(row.Attribute("empno"), Integer) = pk) Select row

        For Each row As XElement In query
            lblEmpNo.Text = row.Attribute("empno")
            lblEmpName.Text = row.Attribute("ename")
            lblJob.Text = row.Attribute("job")
            lblHireDt.Text = row.Attribute("hiredate")
            lblSal.Text = row.Attribute("sal")
            lblDeptNo.Text = row.Attribute("deptno")
            lblDeptName.Text = row.Attribute("dname")
            lblLoc.Text = row.Attribute("loc")
        Next
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Response.Redirect("Employee.aspx")
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions