ARTICLE

Binding XML file to vb.net

Posted by Eddie Jack Articles | Visual Basic Language June 07, 2011
In this article, I will retrieve data from XML file. It is very simple to bind data to asp.net. First you should use add System.Xml namespace because it has a class XmlDocument which will allow us to retrieve data from XML file and it is used to represent xml document.
 
Reader Level:

In this article, I will retrieve data from XML file. It is very simple to bind data to asp.net. First you should use add System.Xml namespace because it has a class XmlDocument which will allow us to retrieve data from XML file and it is used to represent xml document. XML stands for Extensible Markup Language. It stores its data in the form of text. In code we used Load () method of XmlDocument class. It is used to load the xml document from the specified URL because URL gives the location where Xml document exist. URL can be either a local file (data.xml) or the http URL. MapPath return the physical path of file that corresponds to the specified virtual path on the web server and Server gets the server object.

 Defult.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Import Namespace="System.Data" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:Label ID="Label1" runat="server" Text="First Name:"></asp:Label>

    <asp:Label ID="LabelFirstName" runat="server"></asp:Label>

    <br />   

    <br />

    <asp:Label ID="Label2" runat="server" Text="Last Name:"></asp:Label>

    <asp:Label ID="LabelLastName" runat="server"></asp:Label>

</asp:Content>

 

Data.xml

<? xml version="1.0" encoding="utf-8" ?>

<data>

  <value>Mark</value>

  <value>Clark</value>

</data>

 

Default.aspx.vb

Imports System

Imports System.Data

Imports System.Configuration

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports System.Xml

 

Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs)

        Dim myXmlDocument As New XmlDocument()

        myXmlDocument.Load(Server.MapPath("data.xml")) 

        LabelFirstName.Text = myXmlDocument.DocumentElement.ChildNodes(0).InnerText.ToString()

        LabelLastName.Text = myXmlDocument.DocumentElement.ChildNodes(1).InnerText.ToString() 

    End Sub 

End Class

Output :

xml.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor