--Step 1
Add A new folder . [Right click on webapplication2. Add a folder.]. Name it xml folder.
--step 2
--Add a new item. Right click ==> add a new item==>xml file.
Step 3- Write in that xml file as below.
<?xml version="1.0" encoding="utf-8" ?>
<footballplayers>
<players>
<id>1</id>
<Name>Messi</Name>
<country>Argentina</country>
</players>
<players>
<id>2</id>
<Name>Ronaldo</Name>
<country>Portugal</country>
</players>
<players>
<id>3</id>
<Name>Gerrard</Name>
<country>England</country>
</players>
<players>
<id>4</id>
<Name>Casillas</Name>
<country>Spain</country>
</players>
</footballplayers>
step 4- now add a xml datasource
drag and drop the xmldatasource
bind the gridview to the xmldatasource
Add A new folder . [Right click on webapplication2. Add a folder.]. Name it xml folder.
--step 2
--Add a new item. Right click ==> add a new item==>xml file.
Step 3- Write in that xml file as below.
<?xml version="1.0" encoding="utf-8" ?>
<footballplayers>
<players>
<id>1</id>
<Name>Messi</Name>
<country>Argentina</country>
</players>
<players>
<id>2</id>
<Name>Ronaldo</Name>
<country>Portugal</country>
</players>
<players>
<id>3</id>
<Name>Gerrard</Name>
<country>England</country>
</players>
<players>
<id>4</id>
<Name>Casillas</Name>
<country>Spain</country>
</players>
</footballplayers>
step 4- now add a xml datasource
drag and drop the xmldatasource
bind the gridview to the xmldatasource
Xml datasource works only with attributes but not child elements.
Now to Avoid this error. Just change the xml file as below.
<?xml version="1.0" encoding="utf-8" ?>
<footballplayers>
<players id="1" Name="Messi" country="Argentina"/>
<players id="2" Name="Ronaldo" country="Portugal"/>
<players id="3" Name="Gerrard" country="England"/>
<players id="4" Name="Casillas" country="Spain"/>
</footballplayers>
Thats it :) Just run now
=======================================================================
---Use xslt for transformation.
Rewriting is manual and laborious and you may make error. So let the xml be as it is.
Root and Child elements.We will use xslt to transform our xml to attribute.
xslt file
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--First part-->
<xsl:template match="footballplayers">
<footballplayers>
<xsl:apply-templates/>
</footballplayers>
</xsl:template>
<!--second part-->
<xsl:template match="players">
<players>
<xsl:for-each select="*">
<xsl:attribute name="{name()}">
<xsl:value-of select="text()"/>
</xsl:attribute>
</xsl:for-each>
</players>
</xsl:template>
</xsl:stylesheet>
xml file
<?xml version="1.0" encoding="utf-8" ?>
<footballplayers>
<players>
<id>1</id>
<Name>Messi</Name>
<country>Argentina</country>
</players>
<players>
<id>2</id>
<Name>Ronaldo</Name>
<country>Portugal</country>
</players>
<players>
<id>3</id>
<Name>Gerrard</Name>
<country>England</country>
</players>
<players>
<id>4</id>
<Name>Casillas</Name>
<country>Spain</country>
</players>
</footballplayers>
Configure the gridview
Thats it done :)
output in browser
Now the Third way is to do like this below :[in default.aspx.cs] write the code as below.
Remove the xml datasource .
And remove the xslt file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Xml folder/FootballPlayers.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
Thats it . Now run the application.
No comments:
Post a Comment