Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Today i was attempting (for the first time) to make a simple application that will allow me to read XML file into Grid View control (via DataSet), and later on save back any changes back to xml file (Win forms application).
For time being i'm "ignoring" the "save" part, as i'm not able even to get the "view/load" part to work proerply, so i will try to get that working later on once i fix the initial problem i'm seeking help for.

I've tried something thats throwing me exception but since i've never tried binding a dataset to XML i'm probably doing something stupid or missing something...

i've made simple xml file (named it: XMLFile1.xml) with the following content:
<br /><?xml version="1.0" encoding="utf-8" ?><br /><item_list><br />  <item><br />    <title>item 1</title><br />    <des>item 1 description</des><br />  </item><br />  <item><br />    <title>item 2</title><br />    <des>item 2 description</des><br />  </item><br /></item_list> <br />


on the form i added:
1. button with default name of button1.
2. DataGridView with default name of dataGridView1.
3. DataSet with default name of dataSet1.

inside the data grid i've created 2 columns with names "title" & "des".

finally created click event for button with the following:
<br /> private void button1_Click(object sender, EventArgs e)<br /> {<br />     string xmlPath = @"..\..\XMLFile1.xml";<br /><br />     dataSet1.ReadXml( xmlPath, XmlReadMode.ReadSchema );<br />     dataGridView1.DataMember = "item";<br /> }<br />


Now the problem i have is the line:
dataGridView1.DataMember = "item";
is thowing exception of "Child list for field item cannot be created".

The truth is i'm not sure if this error is result of invalid xml file i've created, or i'm not using properly the DataMember property.

I would appreciate if anyone could help me & point me in right direction of what i'm doing wrong here.

best regards,
Idan B.

Posted

1 solution

I just quickly browsed your post, so I may be way off on the answer, but it looks like you are on the right track.

DataSet dsXml = new DataSet();<br />dsXml.ReadXml(fileName);


Within a DataSet you can have multiple DataTable objects depending on the format of the XML. What I've done in the past is to create a dynamic combo box that gets the name of every single table in the DataSet.

You would want to set the DataSource of you DataGridView to the DataSet object we read (dsXml). Then when your combo box changes you can set the data member to the name of the selected item.

private void LoadComboBox()<br />{<br />   foreach (DataTable tbl in dsXml.Tables)<br />   {<br />       ComboBox1.Items.Add(tbl.TableName);<br />   }<br />}<br />private void SetSource()<br />{<br />   DataGridView1.DataSource = dsXml;<br />}<br /><br />private void OnComboChanged()<br />{<br />   DataGridView1.DataMember = ComboBox1.Text;<br />}



You could also set the source to a specific table in the DataSet.

DataGridView1.DataSource = dsXml.Tables[0];
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900