Friday, December 10, 2010

xml

We can create xml files by adding project-->Add new item-->xml file

here is a sample xml file



To access this xml file we can utilise either of the following
1. XML Document class
2.XMLTextReader
3.using DataSet


1.Using XmlDocument Class
using System.Xml;


private void showbtn_Click(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"D:\blessy\Xmldemo\Xmldemo\Stud.xml");
textBox1.Text = xdoc.InnerText;
}

2. printing separate node details using System.Xml;

private void showdetailsbtn_Click(object sender, EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"d:\blessy\Xmldemo\Xmldemo\Stud.xml");

XmlNodeList xlist = xdoc.SelectNodes("students/student");
foreach (XmlNode xn in xlist)
{
listBox1.Items.Add(xn["name"].InnerText);
listBox2.Items.Add(xn["age"].InnerText);
}


3. DataSet can be used to retreive xml information


private void button3_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(@"d:\blessy\Xmldemo\Xmldemo\Stud.xml");
dataGridView1.DataSource = ds.Tables["student"];
}

0 comments:

Post a Comment