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.

    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 btnRead_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();
        }
    }
}

If I understand you correctly, you want to select the xml file path from the listbox. The .SelectedItem.ToString() property should be able to give you the selected file path, instead of looping through all the listbox items.

tinstaafl

Thanks it works after i used .SelectedItem.ToString() property.

You're welcome. Please remember to mark this solved. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.