Breaking News
Loading...
Monday, 10 December 2012

How to delete node (Element) in Xml file in c#.Net

Note :  for this code you have to make on "people1.xml" file on to the desktop.than run this code.

//////////////////////   people1.xml    //////////////////////

<people>
  <person>
    <name>nirav</name>
    <age>20</age>
    <email>n005nirav@gmail.com</email>
  </person>
  <person>
    <name>vijay</name>
    <age>21</age>
    <email>vijay@gmail.com</email>
  </person>
</people>

////////////////////////////////////////////////////////////////////

First create form like :




-  now double click on button and write this code .


/////////////////   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 btndelete_Click(object sender, EventArgs e)
            {
                XmlDocument xdoc1 = new XmlDocument();
                xdoc1.Load("C:\\Users\\Nirav\\Desktop\\people1.xml");
                foreach (XmlNode node in xdoc1.SelectNodes("people/person"))
                {
                    if (node.SelectSingleNode("name").InnerText == textBox4.Text)
                    {
                        node.ParentNode.RemoveChild(node);
                    }
                   
                }
                xdoc1.Save("C:\\Users\\Nirav\\Desktop\\people1.xml");


            }

      }
}


///////////////////////////////////////////////////////////////////////////////////////////////




/////////////   OUTPUT   ////////////////



-  click on button and show result  :





                                                 





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

9 comments:

  1. if we use dropdownlist instead of textbox, how can we write code?

    ReplyDelete
  2. here we tatake Combobox instead of dropdownlist ,so code is look like

    if (node.SelectSingleNode("name").InnerText == combobox1.SelectedItem.ToString())

    ReplyDelete
  3. thank u :)

    if we take xml page like this ok



    nirav
    20
    n005nirav@gmail.com


    vijay
    21
    vijay@gmail.com



    same content we will store in database by creating one table.
    Actually my requirement is if we use dropdownlist control or combobox when we are select that content inside of dropdown simultaniously deleted in xml and database also...

    could you please send me that code.

    thanking u

    ReplyDelete
  4. first you have to make table with follwing coloum name

    ReplyDelete
  5. if your table is something like ...
    name age email
    ---------------------------------
    nirav 21 sdfhjkh@gmail.com
    vijay 20 vijay@gmail.com
    kapil 23 jjj@gmail.com
    -----------------------------------

    then code is something like :

    SqlConnection conn = new SqlConnection("Data Source=NIRAV-PC;Initial Catalog=test_xml;Integrated Security=True");
    conn.Open();
    string str = "delete tbl_xmltest where name='" + comboBox1.SelectedValue.ToString() + "'";
    SqlCommand cmd = new SqlCommand(str, conn);
    int i=cmd.ExecuteNonQuery();
    MessageBox.Show(i.ToString());



    XmlDocument xdoc1 = new XmlDocument();
    xdoc1.Load("C:\\Users\\Nirav\\Desktop\\people1.xml");
    foreach (XmlNode node in xdoc1.SelectNodes("people/person"))
    {
    if (node.SelectSingleNode("name").InnerText == comboBox1.SelectedValue.ToString())
    {
    node.ParentNode.RemoveChild(node);
    }

    }
    xdoc1.Save("C:\\Users\\Nirav\\Desktop\\people1.xml");



    *******************************************************
    if you need more hel then give me your email i send file to your email address
    thanks.......


    ReplyDelete
  6. :) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer

    ReplyDelete
  7. good job excellent.. Thanks for sharing... :)

    ReplyDelete

Thanks for comment

 
Toggle Footer