XML and its implementation


XML stands for extensible markup language.It stores and transports data. The XML format is supported for all applications. It is independent of all software applications.

XML provides the following advantages :

  1. Documents are easily readable and self-describing.
  2. XML is interoperable.
  3. XML Documents can be hierarchical.

Here is an example that tell us the different way to access the contents of XML File.

  1. Show data from XML file with Attribute.
  2. Show data from XML file with Tag.
  3. Show data from XML file with Attribute and Tag.

Alplication::

1. Show data from XML file with Attribute :

1. create a XML file :

<?xml version="1.0" encoding="utf-8" ?>
<batch>
  <student RegNo="S201304001" Name="Deepak"  Address="Taramandal-Gorakhpur" />
  <student RegNo="S201304002" Name="Krishna"  Address="Alinagar-Gorakhpur" />
  <student RegNo="S201304003" Name="Rehan"  Address="Ilahibagh-Gorakhpur" />
  <student RegNo="S201304004" Name="Wajji"  Address="Ramdattpur-Gorakhpur" />
  <student RegNo="S201304005" Name="Mohan"  Address="Daudpur-Gorakhpur" />
  <student RegNo="S201304006" Name="Manish"  Address="Rushtampur-Gorakhpur" />
  <student RegNo="S201304007" Name="Swet Singh"  Address="Rushtampur-Gorakhpur" />
</batch>

2. Create a web form with xmldatasource:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    
    <div style="width:432px; padding:10px; border:1px solid #0066FF; margin:auto;  background-color:#fcfc66" >
        <asp:GridView ID="GridView1" runat="server"  Width="432px" 
            AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
        <AlternatingRowStyle  BackColor="#fc6600" />
            <Columns>
                <asp:BoundField DataField="RegNo" HeaderText="RegNo" SortExpression="RegNo" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Address" HeaderText="Address" 
                    SortExpression="Address" />
            </Columns>
        </asp:GridView>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Data1.xml"></asp:XmlDataSource>
    </div>
    </form>
</body>
</html>

3. Output:
xx1

2. Show data from XML file with Tag:

1. create a XML file :

<?xml version="1.0" encoding="utf-8"?>
<students>
  <student>
    <RegNo>S201304001</RegNo>
    <Name>Deepak </Name>
    <Address>Taramandal-Gorakhpur </Address>
  </student>
  <student>
    <RegNo>S201304002</RegNo>
    <Name>Krishna </Name>
    <Address>Alinagar-Gorakhpur </Address>
  </student>
  <student>
    <RegNo>S201304003</RegNo>
    <Name>Rehan</Name>
    <Address>Ilahibagh-Gorakhpur </Address>
  </student>
  <student>
    <RegNo>S201304004</RegNo>
    <Name>Wajji</Name>
    <Address>Ramdattpur-Gorakhpur </Address>
  </student>
  <student>
    <RegNo>S201304005</RegNo>
    <Name>Mohan</Name>
    <Address>Daudpur-Gorakhpur </Address>
  </student>
  <student>
    <RegNo>S201304006</RegNo>
    <Name>Manish</Name>
    <Address>Rushtampur-Gorakhpur</Address>
  </student>
  <student>
    <RegNo>S201304007</RegNo>
    <Name>Swet Sing</Name>
    <Address>Rushtampur-Gorakhpur </Address>
  </student>
  
</students>
           
           
            

2. Create a web form with xmldatasource:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowXMl2.aspx.cs" Inherits="ShowXMl2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   <div style="width:432px; padding:10px; border:1px solid #0066FF; margin:auto;  background-color:#fcfc66" >
        <asp:GridView ID="GridView1" runat="server"  Width="432px" 
            AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
        <AlternatingRowStyle  BackColor="#fc6600" />
         <Columns>

            <asp:TemplateField HeaderText="RegNo">
            <ItemTemplate>
            <%# XPath("RegNo") %>
             </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField  HeaderText="Name">

            
            <ItemTemplate>
            <%# XPath("Name") %>
            </ItemTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField  HeaderText="Address">
            <ItemTemplate>
            <%# XPath("Address") %>
            </ItemTemplate>
            </asp:TemplateField>

            </Columns>
        </asp:GridView>
       <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Data2.xml"></asp:XmlDataSource>
    </div>
    </form>
</body>
</html>
           
           
            

3. Output:
xx1

3. Show data from XML file with Attribute and Tag:

1. create a XML file :

<?xml version="1.0" encoding="utf-8" ?>
<students>
  <student RegNo="S201304001"  Name="Deepak">
   <Address>Taramandal-Gorakhpur </Address>
  </student>

  <student RegNo="S201304002"  Name="Krishna">
    <Address>Alinagar-Gorakhpur </Address>
  </student>

  <student RegNo="S201304003"  Name="Rehan">
    <Address>Ilahibagh-Gorakhpur </Address>
  </student>

  <student RegNo="S201304004"  Name="Wajji">
    <Address>Ramdattpur-Gorakhpur </Address>
  </student>
  <student RegNo="S201304005"  Name="Mohan">
    <Address>Daudpur-Gorakhpur </Address>
  </student>
  
  <student RegNo="S201304006"  Name="Manish">
      <Address>Rushtampur-Gorakhpur</Address>
  </student>
  <student RegNo="S201304007"  Name="Swet Singh">
    <Address>Rushtampur-Gorakhpur </Address>
  </student>
</students>

2. Create a web form with xmldatasource:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowXML3.aspx.cs" Inherits="ShowXML3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   <div style="width:432px; padding:10px; border:1px solid #0066FF; margin:auto;  background-color:#fcfc66" >
        <asp:GridView ID="GridView1" runat="server"  Width="432px" 
            AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
        <AlternatingRowStyle  BackColor="#fc6600" />
         <Columns>

            <asp:TemplateField HeaderText="RegNo">
            <ItemTemplate>
            <%# XPath("@RegNo") %>
             </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField  HeaderText="Name">

            
            <ItemTemplate>
            <%# XPath("@Name") %>
            </ItemTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField  HeaderText="Address">
            <ItemTemplate>
            <%# XPath("Address") %>
            </ItemTemplate>
            </asp:TemplateField>

            </Columns>
        </asp:GridView>
       <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Data3.xml"></asp:XmlDataSource>
    </div>
    </form>
</body>
</html>
           

3. Output:
xx1