ASP.Net/XML/XML GridView
asp:GridView column alignment
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Configuration"%> <%@ Import Namespace="System.Data"%> <script runat="server">
void Page_Load(Object sender, EventArgs e) { DataSet authorsDataSet; string filePath = Server.MapPath("Authors.xml"); authorsDataSet = new DataSet(); //Read the contents of the XML file into the DataSet authorsDataSet.ReadXml(filePath); authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView; authorsGird.DataBind(); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Reading XML Data into a DataSet object </title>
</head> <body>
<form id="form1" runat="server">
<asp:GridView id="authorsGird" runat="server" AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True"> <Columns> <asp:BoundField HeaderText="Last Name" DataField="lastName" /> <asp:BoundField HeaderText="First Name" DataField="firstName" ItemStyle-HorizontalAlign="Right" /> </Columns> </asp:GridView>
</form>
</body> </html>
</source>
Displaying XML Data in a GridView and a ListBox?
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head>
<title>Displaying XML Data in a GridView and a ListBox</title>
</head> <body>
<form id="form1" runat="server">
<asp:ListBox ID="lstTitles" Runat="server" DataSourceID="bookSource" DataValueField="ISBN" DataTextField="Title"/>
<asp:GridView ID="bookView" Runat="server" DataSourceID="bookSource" AutoGenerateColumns="False"> <Columns> <asp:BoundField HeaderText="ISBN" DataField="ISBN" SortExpression="ISBN"></asp:BoundField> <asp:BoundField HeaderText="Title" DataField="Title" SortExpression="Title"></asp:BoundField> <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price"></asp:BoundField> </Columns> </asp:GridView>
<asp:XmlDataSource ID="bookSource" Runat="server" DataFile="~/Data.xml" XPath="Data/genre[@name ="Fiction"]/book"></asp:XmlDataSource>
</form>
</body> </html> File: ~/Data.xml
<genre name="Fiction"> <book ISBN="1" Title="title 1" Price="19.99" Discount="1.999"> <chapter num="1" name="Introduction"> Abstract... </chapter> <chapter num="2" name="Body"> Abstract... </chapter> <chapter num="3" name="Conclusion"> Abstract... </chapter> </book> </genre> <genre name="NonFiction"> <book ISBN="2" Title="title 2" Price="27.95" Discount="2.795"> <chapter num="1" name="Introduction"> Abstract... </chapter> <chapter num="2" name="Body"> Abstract... </chapter> <chapter num="3" name="Conclusion"> Abstract... </chapter> </book> </genre>
</source>
Load XML data to GridView
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Grades" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Grades</title>
</head> <body>
<form id="form1" runat="server">
<asp:Button ID="btnViewGrades" runat="server" OnClick="btnViewGrades_Click" Text="View Grades" />
<asp:GridView ID="GridView1" runat="server"/> <asp:Label ID="lblResult" runat="server"></asp:Label>
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Security.Principal; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Grades : System.Web.UI.Page {
protected void btnViewGrades_Click(object sender, EventArgs e) { Response.Write(WindowsIdentity.GetCurrent().Name + "
"); WindowsImpersonationContext ctx = ((WindowsIdentity)User.Identity).Impersonate(); Response.Write(WindowsIdentity.GetCurrent().Name + "
"); try { DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("Data.xml")); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } catch (UnauthorizedAccessException) { lblResult.Text = "Not Authorized!"; } ctx.Undo(); Response.Write(WindowsIdentity.GetCurrent().Name + "
"); }
} File: Data.xml <?xml version="1.0" encoding="utf-8" ?> <students>
<student> <name>John Smith</name> <grade>B</grade> </student> <student> <name>Jane Doe</name> <grade>A</grade> </student>
</students>
</source>