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>

How to Convert Excel Data into XML Document





using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using Excel = Microsoft.Office.Interop.Excel;

using System.Xml;

namespace EXCELTOXML

{

    public partial class Form1 : Form

    {

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

        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {



        }



        private void button1_Click(object sender, EventArgs e)

        {



            Excel.Application xlApp;



            Excel.Workbook xlWorkBook;



            Excel.Worksheet xlWorkSheet;



            Excel.Range range;







            int rw = 0;



            int cl = 1;







            xlApp = new Excel.Application();



            xlWorkBook = xlApp.Workbooks.Open(@"C:\images\docs\abc.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);



            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);







            range = xlWorkSheet.UsedRange;



            rw = range.Rows.Count;





            //



            //data read code...............................................



            for (int i = 2; i <= rw; i++)

            {



                student s = new student();



                s.id = (int)(range.Cells[i, 1] as Excel.Range).Value;



                s.Name = (string)(range.Cells[i, 2] as Excel.Range).Value;



                s.Marks = (int)(range.Cells[i, 3] as Excel.Range).Value;



               



                li.Add(s);



            }







            //data read code...............................................







            //



            xlWorkBook.Close(true, null, null);



            xlApp.Quit();







            Marshal.ReleaseComObject(xlWorkSheet);



            Marshal.ReleaseComObject(xlWorkBook);



            Marshal.ReleaseComObject(xlApp);







            dataGridView1.DataSource = li;





        }



        private void button2_Click(object sender, EventArgs e)

        {







            XmlTextWriter writer = new XmlTextWriter(@"C:\images\docs\salman.xml", System.Text.Encoding.UTF8);

            writer.WriteStartDocument(true);

            writer.Formatting = Formatting.Indented;

            writer.Indentation = 2;

            writer.WriteStartElement("Students");

            foreach (var item in li)

            {

                createnode(item, writer);



            }





            writer.WriteEndElement();

            writer.WriteEndDocument();

            writer.Close();

            MessageBox.Show("XML File created ! ");



        }



        private void createnode(student s, XmlTextWriter writer)

        {

            writer.WriteStartElement("Student");

            //id...............................

            writer.WriteStartElement("id");

            writer.WriteString(s.id.ToString());

            writer.WriteEndElement();

            //................name



            writer.WriteStartElement("Name");

            writer.WriteString(s.Name.ToString());

            writer.WriteEndElement();





            //................name



            writer.WriteStartElement("Marks");

            writer.WriteString(s.Marks.ToString());

            writer.WriteEndElement();



       







            writer.WriteEndElement();

       

        }

    }

}


How to export Sql Database records into Excel File







using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using Excel = Microsoft.Office.Interop.Excel;

namespace sqltoExcel

{

    public partial class Form1 : Form

    {

        private string connctionstring = "Data Source=faculty-35;Initial Catalog=exceltosql;Persist Security Info=True;User ID=sa;Password=aptech";



        public Form1()

        {

            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)

        {

            // TODO: This line of code loads data into the 'exceltosqlDataSet.tbl_student' table. You can move, or remove it, as needed.

            this.tbl_studentTableAdapter.Fill(this.exceltosqlDataSet.tbl_student);



        }



        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection cnn;

     

            string sql = null;

            string data = null;

            int i = 0;

            int j = 0;



            Excel.Application xlApp;

            Excel.Workbook xlWorkBook;

            Excel.Worksheet xlWorkSheet;

            object misValue = System.Reflection.Missing.Value;



            xlApp = new Excel.Application();

            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);





            cnn = new SqlConnection(connctionstring);

            cnn.Open();

            sql = "SELECT * FROM tbl_student";

            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);

            DataSet ds = new DataSet();

            dscmd.Fill(ds);



            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

            {

                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)

                {

                    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();

                    xlWorkSheet.Cells[i + 1, j + 1] = data;

                }

            }



            xlWorkBook.SaveAs(@"C:\images\docs\studens.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

            xlWorkBook.Close(true, misValue, misValue);

            xlApp.Quit();



            releaseObject(xlWorkSheet);

            releaseObject(xlWorkBook);

            releaseObject(xlApp);



            MessageBox.Show("Excel file created , you can find the file ");











        }



        private void releaseObject(object obj)

        {

            try

            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

                obj = null;

            }

            catch (Exception ex)

            {

                obj = null;

                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

            }

            finally

            {

                GC.Collect();

            }

        }









    }

}


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 ...