dotnet xml:
I must be missing something simple. I want to serialize a DataSet to local
disk when my .NET 2.0 WinForms app closes, and deserialize the DataSet when
the app reopens.
I use this code to serialize my DataSet:
XmlSerializer ser = new XmlSerializer(typeof(DataSet));
TextWriter writer = new StreamWriter(xmlFile);
ser.Serialize(writer, projectData);
projectData.WriteXmlSchema(xmlSchema); //must this be written every time?
writer.Close();
Here are the results:
<?xml version="1.0" encoding="utf-8"?>
<DataSet>
<xs:schema id="ProjectData" xmlns=""
xmlns:xs="
http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ProjectData" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="TableProject">
<xs:complexType>
<xs:sequence>
<xs:element name="Project_ID" msdata:AutoIncrement="true"
type="xs:int" />
etc.
etc.
etc.
and this code to deserialize it:
projectData = new DataSet();
StringReader xmlReader = new StringReader(xmlFile);
projectData.ReadXml(xmlReader);
xmlReader.Close();
But the deserialization code barfs on the first line of the xml file.
I believe I am missing some namespace reference to the xsd file, or
something like that. How can I adjust my serialization code to include the
proper information/formatting in the xml file? Do I need to write the
schema to disk first? I want to be able to programmatically recreate the
xmlFile and the xsdSchema whenever I want (they will always have the same
definition, it's just that the files may get deleted and need to be
recreated).
Thanks in advance.