Loading a datatable into a xml and xml back into a datatable

Hatlevik

New Member
So I have a I query data into a DataTable\[code\]using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)){ DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "AccessRights"); return dataSet.Tables[0];}\[/code\]Now I start to construct the XML to send back to the client\[code\]string tableData = http://stackoverflow.com/questions/12455539/null;using(StringWriter sw = new StringWriter()){ rightsTable.WriteXml(sw); tableData = sw.ToString(); } StringBuilder build = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(build, new XmlWriterSettings { OmitXmlDeclaration = true })) { writer.WriteStartElement("useraccess"); writer.WriteCData(xmlTable.OuterXml); writer.WriteEndElement(); }\[/code\]Now on the Client I need to get the DataTable from the Xml\[code\]XmlCDataSection cDataNode = (XmlCDataSection)xdoc.SelectSingleNode("./mp_getindividualaccessrights_response/data/useraccess").ChildNodes[0];string xmlDataString = cDataNode.Data.ToString();StringReader sReader = new StringReader(xmlDataString);string s = sReader.ToString();DataSet ds = new DataSet();ds.ReadXml(sReader);return ds.Tables[0];\[/code\]This works if there is data in the database which will return the columns and rows in the DataTableMy Problem comes in if there are no results in the DataBase, then I want the columns to be stored in the xml string (When dt.WriteXml()) even though there is no row data. But What happens is when dt.WriteXml() the xml string that is return is an empty tag which I do not want. Basically I want the tag to have the columns in the database query even though there are no data rows e.g. I know for a fact the DataTable has columns (dt.Tables[0].Columns.Count) I get a number greater then 0 and when I (dt.Tables[0].Rows.Count) I get 0. Which is fine cause When I queried I did not find the results I was looking for but the columns are still present. But the moment I DataTable.WriteXML() the columns disappear.I want the columns to be displayed in the xml string regardless or rows were found or not. But DataTable.WriteXml does not seem to be doing this correctly.Can some one please assist me on this issue.
 
Back
Top