Breaking News
Loading...
Sunday 9 December 2012

Read Xml File From PC in C#.Net


First make Form like below in MS Visual stdio 2008.




Here code is given below for reading an xml file from your computer in c#.net ,you can red only one nod or multiple node at a time both are given.


NOTE : for exicuting this code you havve to make first xml file like as shown below

    
  <people>
  <person>
    <name>dscsd</name>
    <age>100</age>
    <email>n005nirav@gmail.com</email>
  </person>
  <person>
    <name>aaa</name>
    <age>10</age>
    <email>aaaaa@gmail.com</email>
  </person>
  <person>
    <name>xyz</name>
    <age>15</age>
    <email>n0dsfv@gmail.com</email>
  </person>
</people>



/////////////////////   CODE    /////////////////////////


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Xml_File_Operation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "XML |*.xml";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                /************ Reading one node of xml file ***********/

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(ofd.FileName);
                MessageBox.Show(xdoc.SelectSingleNode("people/person/name").InnerText);


            }

        }


             /************ Reading multiple node of xml file ***********/

        private void btnmultinode_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "XML |*.xml";
            if (ofd.ShowDialog() == DialogResult.OK)
            {

               


                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(ofd.FileName);

                foreach (XmlNode node in xdoc.SelectNodes("people/person"))
                {
                    MessageBox.Show(node.SelectSingleNode("name").InnerText);
                }


            }


        }
    }
}




--
/\/ir@\/  <(.'.)>

0 comments:

Post a Comment

Thanks for comment

 
Toggle Footer