Vous êtes sur la page 1sur 3

using using using using using using using using using

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

namespace Window_XML { public partial class Form1 : Form { public Form1() { InitializeComponent(); } XmlDocument doc = new XmlDocument(); //creating obj for xml document private void button1_Click(object sender, EventArgs e) { //XmlDocument doc = new XmlDocument(); doc.Load("Student.xml"); //MessageBox.Show(doc.OuterXml); //It will displays the Full XML Doc ument (Total document Student.xml) XmlElement elmstuds = doc.DocumentElement; //It will fetch only from root node (Starting from <Students>...</Students>) //MessageBox.Show(elmstuds.OuterXml); //It will display from root no de XmlElement elmstud = (XmlElement)elmstuds.ChildNodes[0]; //fetch the data of element node based on Index //MessageBox.Show(elmstud.OuterXml); //Displays the Element Node Dat a <Student>....</Student> XmlElement elmname = (XmlElement)elmstud.ChildNodes[0]; //fetch the data of child node of element node based on Index //MessageBox.Show(elmname.OuterXml); //Displays <Name>....</Name> XmlText elmtxt = (XmlText)elmname.FirstChild; //fetches the text of child node of element node based on Index //MessageBox.Show(elmtxt.OuterXml); //Displays Name of the student = "Student1" XmlAttribute elmroll = elmstud.GetAttributeNode("Rollno"); //fetches the attribute values of child node of element node based on Index //MessageBox.Show(elmroll.Value); //Displays the Rollno value as 100 //MessageBox.Show(elmroll.Value +" "+ elmstuds.ChildNodes[0].FirstCh ild.FirstChild.Value); } XmlElement elmstuds; private void Form1_Load(object sender, EventArgs e)

{ doc.Load("Student.xml"); elmstuds = doc.DocumentElement; foreach (XmlElement elmstud in elmstuds.ChildNodes) comboBox1.Items.Add(elmstud.GetAttributeNode("Rollno")); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { int ind = comboBox1.SelectedIndex; XmlElement elmstud = (XmlElement)elmstuds.ChildNodes[ind]; txtName.Text = elmstud.FirstChild.FirstChild.Value; txtResult.Text = elmstud.LastChild.FirstChild.Value; } private void btnSubmit_Click(object sender, EventArgs e) { XmlElement enstud, enname, enres; XmlText tnname, tnresult; XmlAttribute anid; enstud = doc.CreateElement("Student"); enname = doc.CreateElement("Name"); enres = doc.CreateElement("Result"); tnname = doc.CreateTextNode(txtName.Text); tnresult = doc.CreateTextNode(txtResult.Text); anid = doc.CreateAttribute("Rollno"); elmstuds.AppendChild(enstud); enstud.AppendChild(enname); enstud.AppendChild(enres); enname.AppendChild(tnname); enres.AppendChild(tnresult); enstud.SetAttributeNode(anid); doc.Save("Student.xml"); comboBox1.Items.Add(comboBox1.Text); MessageBox.Show("New Node Added with ID: "+comboBox1.Text); } private void btndelete_Click(object sender, EventArgs e) { int ind = comboBox1.SelectedIndex; XmlElement elmstud = (XmlElement)elmstuds.ChildNodes[ind]; elmstud.ParentNode.RemoveChild(elmstud); comboBox1.Items.Remove(comboBox1.Text); doc.Save("Student.xml"); } private void btnupdate_Click(object sender, EventArgs e) { int ind = comboBox1.SelectedIndex; XmlElement elmstud = (XmlElement)elmstuds.ChildNodes[ind]; elmstud.FirstChild.FirstChild.Value = txtName.Text; elmstud.LastChild.FirstChild.Value = txtResult.Text; doc.Save("Student.xml");

} private void btndisp_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); ds.ReadXml("Student.xml"); dataGrd.DataSource = ds.Tables[0]; } } }

Vous aimerez peut-être aussi