Click here to Skip to main content
13,045,657 members (69,038 online)
Rate this:
Please Sign up or sign in to vote.
I am trying to write a data table to xml and again reading it back to my application.

This is the structure of my DataTable ---> Note : It is having a column of DataTable type.

 DataTable _clsdatatable = new DataTable();            
_clsdatatable.Columns.Add("WIZARDID", typeof(int));
_clsdatatable.Columns.Add("WIZARD_NAME", typeof(string));
_clsdatatable.Columns.Add("WIZARD_TYPE", typeof(string));
_clsdatatable.Columns.Add("CREATE_DATE", typeof(DateTime));
_clsdatatable.Columns.Add("UPDATE_DATE", typeof(DateTime));
_clsdatatable.Columns.Add("USERID", typeof(short));
_clsdatatable.Columns.Add("WIZARD_DATA", typeof(DataTable));

Now if I try to export this data table to XML, I am getting a complex xml with so many diffgram data and schema details.

Now if I try to read the xml it is giving me error as

Column 'WIZARDID' does not belong to table Wizard.

If there is no data table present in the parent data table then it is working fine both > write and read case. So seems like the problem is with writing XML process while Data table is present inside data table.

Please let me know if anybody can help me with this --- Is there any way to attach the xml file with the post --- then it would be helpful for you gyus.
Posted 29-May-13 0:43am
tumbledDown2earth 29-May-13 7:41am
Did you gove the table a name "Wizard"? It is not reflecting in the code above
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

 // By using this method we can convert datatable to xml
public string ConvertDatatableToXML(DataTable dt)
MemoryStream str = new MemoryStream();
dt.WriteXml(str, true);
str.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(str);
string xmlstr;
xmlstr = sr.ReadToEnd();
return (xmlstr);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I have solved this ---

The trick was not to use WriteXMl() method for datatabel -> xml.
Instead i used loadxml() and then when I tried to read that xml by ReadXML() method it worked fine.

Also the same can be possible with binary serialization.

--- Any way thanks for your comments.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100