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

Dani AI

Generated

Following 's suggestion, using the selected ListBox item instead of looping through every entry is the right approach. For , an important distinction: SelectedItem.ToString() returns the ListItem's Text (what the user sees). When a ListBox is data-bound with DataTextField and DataValueField, the actual file path often lives in SelectedValue (or SelectedItem.Value). Always check if (ListBox1.SelectedItem != null) before using it to avoid null-reference errors.

Practical checks that often fix silent failures: confirm the ListBox contains full, server-accessible paths (or call Server.MapPath / Path.Combine as appropriate), verify File.Exists(xmlPath) before attempting to load, and ensure the ASP.NET process has read permission on the folder. Note that files on a client PC are not directly readable by the server — they must be uploaded or otherwise accessible to the server process.

Binding tips to keep the GridView predictable: prefer binding a single DataTable (e.g., the table you actually want) rather than the whole DataSet with a DataMember, and check ds.Tables.Count > 0 before indexing. If the XML is hierarchical or produces multiple tables, shape it first with LINQ to XML or XmlDataSource to get the exact columns needed. Also avoid repopulating the ListBox on every postback — populate it inside if (!IsPostBack).

Small bug to watch for in the original loop: "Processing file: " + ix + 1 concatenates incorrectly; use parentheses so arithmetic happens first, e.g. "Processing file: " + (ix + 1). Overall, selecting the single file path from the ListBox and validating it before binding will make the GridView update reliably — credit to for the key hint.

Recommended Answers

All 3 Replies

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.