Friday, November 26, 2010

How to Read and Get Data From XML Document in C#

By this Method we can easily get Data from XML Database. For this first of all create a "ContactsList.xml" XML file and under root section add your recoreds.

public void GetData()
{
try
{
string path = "ContactsList.xml";
FileStream fs = new FileStream path,FileMode.Open,FileAccess.Read, FileShare.ReadWrite);
DataSet ds = new DataSet();
ds.ReadXml(fs);
if (ds.Tables.Count != 0)
{
DataTable dt = ds.Tables[0];

int a = dt.Rows.Count;

for (int i = 0; i < a; i++)
{
string[] str1 = new string[4];
str1[0] = dt.Rows[i].ItemArray[0].ToString();


str1[1] = dt.Rows[i].ItemArray[1].ToString();
str1[2] = dt.Rows[i].ItemArray[2].ToString();
int count = Convert.ToInt32(dt.Rows[i].ItemArray[7].ToString());
if (count.Equals(1) || count.Equals(0))
{
str1[3] = dt.Rows[i].ItemArray[3].ToString();
ListViewItem item1 = new ListViewItem(str1);
ldtvw_Contact.Items.Add(item1);
}
else
{
string interests = dt.Rows[i].ItemArray[3].ToString() + "," + dt.Rows[i].ItemArray[4].ToString() + "," + dt.Rows[i].ItemArray[5].ToString() + "," + dt.Rows[i].ItemArray[6].ToString();
str1[3] = interests;
ListViewItem item1 = new ListViewItem(str1);
ldtvw_Contact.Items.Add(item1);
}

}

fs.Close();
}

Thats All....

No comments:

Post a Comment