newbie: How to use WriteXmlSchema?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.


.



Relevant Pages

  • Re: Serialize System.Windows.Forms.Control
    ... reparsing the information from the xml file to customly "deserialize" ... WPF uses XAML to instantiate its UI which of course you can then serialize back to XAML. ... For now I dont have any other choice that parse each object/properties ... One by One and store it in an xml file by myself... ...
    (microsoft.public.dotnet.languages.csharp)
  • To Serialize or Not to Serialize?
    ... I keyed in the VB samples and the Serialize ... But if you try to feed the output XML file created ... with the Serialize sample to the Deserialize sample, ... Public Sub Calculate ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Serialize multiple object in the same XML file
    ... The way I am using is creating an class which has all the objects I want to serialize. ... Create a collection of objects and serialize it to a xml file. ... WHen you deserialize, ... Actualy, when i serialize the first object in XML file, it work fine, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: deserializing a collection which implements collectionbase
    ... The side effect of this is that your collection will now serialize as ... > I have a collection calls Reports made up of Report objects. ... > I want to be able to deserialize the file in the Reports collection constructor and build the Report objects on the fly. ... The only examples I've been able to find read the entire XML file outside of the class, but I want to be able to be able to dynamically build everything inside the collection class. ...
    (microsoft.public.dotnet.xml)
  • Re: deserializing a collection which implements collectionbase
    ... The side effect of this is that your collection will now serialize as ... > I have a collection calls Reports made up of Report objects. ... > I want to be able to deserialize the file in the Reports collection constructor and build the Report objects on the fly. ... The only examples I've been able to find read the entire XML file outside of the class, but I want to be able to be able to dynamically build everything inside the collection class. ...
    (microsoft.public.sqlserver.xml)