Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
tumbledDown2earth 29-May-13 7:41am    
Did you gove the table a name "Wizard"? It is not reflecting in the code above

C#
 // 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);
}
 
Share this answer
 
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.
 
Share this answer
 

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