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 :