Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

(I am a noob to c#)
reading from a .xml file
I need to get certain coloms from a data set into different tables.
splitting the file into 3 tables.
any ideas would help.
C#
DataSet mydata = new DataSet("mydata");
mydata.ReadXml("C:\\Program Files (x86)\\MaxID\\MaxID Fastskan Driver\\Data\\FastSkanDriver.xml");
dataGridView1.DataSource = mydata.Tables[0];

Say I have data "id number" "name" "address" "tel number" ect. but only want address and tel number in table 1 and name and address in table 2.
Posted
Updated 31-Oct-11 22:02pm
v3
Comments
RaisKazi 1-Nov-11 3:57am    
Your Question is unclear. Please elaborate.
Nic'Kemp 1-Nov-11 4:06am    
say i have 10 coloms of data that need to be split into two tables
datset name surname adress telnumber idnumber

i want to split maybe name and adress into table1
and name and idnumber into table2
CodingLover 1-Nov-11 4:01am    
Break down your question into simple steps, if you are not clear with the complete. Then address a step at a time. If you could explain each step clearly we can comment easily.
Nic'Kemp 1-Nov-11 4:18am    
say i have 10 coloms of data that need to be split into two tables datset name surname adress telnumber idnumber i want to split maybe name and adress into table1 and name and idnumber into table2

Try as below code.
C#
DataSet mydata = new DataSet("mydata");
mydata.ReadXml("YourXmlFilePath");

DataTable firstDataTable = new DataTable();
DataTable secondDataTable = new DataTable();

if (mydata.Tables.Count > 0)
{
  firstDataTable = mydata.Tables[0].Copy();
  secondDataTable = mydata.Tables[0].Copy();

  //Remove columns from the first DataTable which you want to remove.
  firstDataTable.Columns.Remove("ColumnNameToRemove1");

  //Remove columns from the second DataTable which you want to remove.
  secondDataTable.Columns.Remove("ColumnNameToRemove2");
}
 
Share this answer
 
Comments
BobJanova 1-Nov-11 6:46am    
Good, although if it is a large data set it would make more sense to do everything on the first table then everything on the second so you don't have three copies at once (or just use Tables[0] as one of the tables directly).
RaisKazi 1-Nov-11 6:49am    
Thank you Bob. :)
Nic'Kemp 1-Nov-11 10:02am    
Thank you RaisKasi
do I then just put the datatables back into datasets to gridview them?

RaisKazi 1-Nov-11 10:05am    
You are welcome. :)
No need to do that, Just assign DataTable to your GridView as below.
dataGridView1.DataSource = firstDataTable;
Nic'Kemp 1-Nov-11 10:32am    
Thank you you have been a great help!!!!!!!
You could read the XML file 'manually' and transfer each item into the appropriate dataset which you can then bind to the relevant datagrid. Alternatively you could take your dataset as created above and manually copy columns of data from that into other datasets. In either case it requires a bit of work.
 
Share this answer
 
Comments
Nic'Kemp 1-Nov-11 5:13am    
How would i go about copying data manualy from the dataset to other datasets
Richard MacCutchan 1-Nov-11 5:29am    
Well I would start at the documentation and see what functions are available to extract columns or cells and copy them to columns or cells elsewhere, and use that information to copy them to another dataset.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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