XML Deserialization of DataTable or DataSet

c#, datatable, serialization, wpf, xml

Solution

Managed to solve it. Seems like when we directly read to an existing `DataSet` using its `ReadXml()` method, it just works.

var dataSet = new DataSet();
dataSet.ReadXml(reader);

Now I can reach the table as `dataSet.Tables[0]`

Problem

I have the following XML: ``` <NewDataSet> <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Data" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Data"> <xs:complexType> <xs:sequence> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> </NewDataSet> ``` I know this was created by serializing a DataTable with `new XmlSerializer(typeof(DataTable)).Serialize(writer, (DataTable)myDataTable);`. The schema as well as the actual data can be different in them. I need to deserialize it, and tried the following: ``` reader.ReadStartElement("NewDataSet"); var dataSet = (DataTable)new XmlSerializer(typeof(DataTable)).Deserialize(reader); reader.ReadEndElement(); ``` and also: ``` reader.ReadStartElement("NewDataSet"); var dataSet = (DataSet)new XmlSerializer(typeof(DataSet)).Deserialize(reader); reader.ReadEndElement(); ``` All I got was System.InvalidOperationException was unhandled Message=There is an error in XML document (26, 14). Source=System.Xml InnerException: "http://www.w3.org/2001/XMLSchema'> was not expected." How can I deserialize that thing?

Original source