Friday, 31 October 2014

Read XML and group using Generic in Asp.Net Using C#

Create new asp.net web application.
Add your xml file.Name it : abc.xml
Create new class file name it MyDTO.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ReadXML
{
public class MyDTO
{
public string Date { get; set; }
public string DESCRIPTION { get; set; }
public string Rev { get; set; }
public string With { get; set; }
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

Now Add new web form to show xml data in grid.

Open Webform html view.
Use below code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ReadXML.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Visible="true"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="DESCRIPTION" HeaderText="DESCRIPTION" SortExpression="DESCRIPTION" />
<asp:BoundField DataField="REv" HeaderText="Rev" SortExpression="Rev" />
<asp:BoundField DataField="With" HeaderText="With" SortExpression="With" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
view raw gistfile1.html hosted with ❤ by GitHub

Now open webform.cs file.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Generic;
namespace ReadXML
{
public partial class WebForm1 : System.Web.UI.Page
{
ReadXML.TcecDTO oUserInfoDTO = new ReadXML.TcecDTO();
List<ReadXML.TcecDTO> Mylist = new List<ReadXML.TcecDTO>();
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GridLoad();
GridView1.DataBind();
Response.Write(Mylist);
}
public DataView GridLoad()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("abc.xml"));
DataTable dt = new DataTable("test");
dt.Columns.Add("Date", typeof(System.String));
dt.Columns.Add("DESCRIPTION", typeof(System.String));
dt.Columns.Add("Rev", typeof(System.String));
dt.Columns.Add("With", typeof(System.String));
DataRow dr;
XmlNode root = doc.DocumentElement;
int k = root.ChildNodes.Count;
for (int j = 0; j < k; j++)
{
dr = dt.NewRow();
dr["Date"] = root.ChildNodes[j].Attributes[0].ChildNodes[0].InnerText;
dr["DESCRIPTION"] = root.ChildNodes[j].Attributes[1].ChildNodes[0].InnerText;
dr["Rev"] = root.ChildNodes[j].Attributes[2].ChildNodes[0].InnerText;
dr["With"] = root.ChildNodes[j].Attributes[2].ChildNodes[0].InnerText;
dt.Rows.Add(dr);
oUserInfoDTO.Date = root.ChildNodes[j].Attributes[0].ChildNodes[0].InnerText;
oUserInfoDTO.DESCRIPTION = root.ChildNodes[j].Attributes[1].ChildNodes[0].InnerText;
oUserInfoDTO.Rev = root.ChildNodes[j].Attributes[2].ChildNodes[0].InnerText;
oUserInfoDTO.With = root.ChildNodes[j].Attributes[2].ChildNodes[0].InnerText;
Mylist.Add(oUserInfoDTO);
}
return dt.DefaultView;
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub