Monday, 25 June 2018

Read Xml File in a gridview in Asp net







using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;



namespace readxmldata

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        List<student> li = new List<student>();

        protected void Page_Load(object sender, EventArgs e)

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(Server.MapPath("~/student.xml"));

            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Students/student");



            foreach (XmlNode node in nodeList)

            {

                student s = new student();



               s.id = Convert.ToInt32(node.SelectSingleNode("id").InnerText);

               s.name = node.SelectSingleNode("name").InnerText;

               s.age = Convert.ToInt32(node.SelectSingleNode("AGE").InnerText);

               li.Add(s);

            }



            GridView1.DataSource = li;

            GridView1.DataBind();









        }

    }

}

-------------------------------------------------------------

class code:

------------------------------------------------------------

    public class student

    {



        public int id { get; set; }

        public string name { get; set; }



        public int age { get; set; }

       

    }

Xml:

------------------------------------------------------------

<?xml version="1.0" encoding="utf-8" ?>

<Students>

  <student>

    <id>1 </id>

    <name>Ali</name>

    <AGE>34</AGE>

  </student>





  <student>

    <id>2 </id>

    <name>AHMED</name>

    <AGE>31</AGE>

  </student>





  <student>

    <id>3 </id>

    <name>SAMI</name>

    <AGE>35</AGE>

  </student>



  <student>

    <id>4 </id>

    <name>BASIT</name>

    <AGE>34</AGE>

  </student>

 

  <student>

    <id>5 </id>

    <name>salman</name>

    <AGE>35</AGE>

  </student>







</Students>

No comments:

Post a Comment

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...