Fulufhelo Munyai

Fulufhelo Munyai

  • NA
  • 33
  • 3.1k

Reading XML file which is on listbox and bind data to Grid

Jul 24 2014 6:46 AM
Hi guys i need your help, i am trying to read an xmlfile which is on the listbox, after reading it i want to bind the data to GridView, i can read directly from the location where i have saved the XMLfiles but now i want to select it from the listBox and display to GridView. The folowing is the code that i did to put the xmlFile to the ListBox
 
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;

namespace ReadXML
{
public partial class ReadXML : System.Web.UI.Page
{
string mypath = "C:/Users/PraiseM/Desktop/Read/";
string[] filepaths = null;
OleDbDataAdapter Adpt = null;
DataTable Tab1 = null;
DataTable Tab2 = null;
DataSet Set1 = null;
DataSet Set2 = null;
protected void Page_Load(object sender, EventArgs e)
{
}

private void readXMLFiles()
{
string filter = "*.xml";
ListBox1.Items.Clear();
filepaths = Directory.GetFiles(mypath, filter);
foreach (string str in filepaths)
{
ListBox1.Items.Add(str);
}
lblFileCount.Text = "Number of Files: " + ListBox1.Items.Count.ToString();
}

private void readmyxml()
{
try
{
if (ListBox1.Items.Count != 0)
{
Tab1 = new DataTable();
Set1 = new DataSet();
Adpt = new OleDbDataAdapter();
//Adpt = myDataAdapter();
Adpt.Fill(Tab1);
Int64 j = 0;

for (int ix = 0; ix < ListBox1.Items.Count; ix++)
{
lblMessage.Text = "Processing file: " + ix + 1;
string xmlfile = ListBox1.Items[ix].Text;
try
{
DataSet ds = new DataSet();
ds.ReadXml(xmlfile, XmlReadMode.InferSchema);
GridView1.DataSource = ds;
GridView1.DataMember = ds.Tables[ix].TableName;
GridView1.DataBind();
Set2 = new DataSet();
Set2.ReadXml(xmlfile, XmlReadMode.InferSchema);
Tab2 = new DataTable();
Tab2 = Set2.Tables[0];
{

j++;

for (int i = 0; i < Tab2.Columns.Count; i++)
{
string xx = Tab2.Columns[i].ToString();
string yy = Tab2.Rows[0][xx].ToString();
}
}
}
catch (Exception exp2)
{
j++;
Debug.Print("Error in " + xmlfile + "\n\n" + exp2.Message);
}

}
lblMessage.Text = "Done!";
}
else
{
lblMessage.Text = "List is empty!";
}
}
catch (Exception e)
{
lblMessage.Text += "Exception: " + e.ToString();
}

}

protected void btnExract0_Click1(object sender, EventArgs e)
{
readXMLFiles();
}

protected void btnExract_Click(object sender, EventArgs e)
{
//DataSet dataSet = new DataSet();
//dataSet.ReadXml("C:\\Users\\PraiseM\\Desktop\\Read\\Test.xml");
//ListBox1.DataSource = dataSet.Tables[0];
//GridView1.DataBind();
}
}
}

Answers (4)