all groups > dotnet xml > february 2006 >
You're in the

dotnet xml

group:

newbie: How to use WriteXmlSchema?


newbie: How to use WriteXmlSchema? deko
2/23/2006 5:32:23 PM
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.

Re: newbie: How to use WriteXmlSchema? deko
2/24/2006 8:24:54 AM
[quoted text, click to view]

Hi and thanks for the reply.

xmlFile is the full path to the xml file (c:\theFile.xml) that holds the
output of the
serialized DataSet.

I thought there might be something wrong with the root node:

[quoted text, click to view]

So you're suggesting that the root node is okay, and the problem is with the
code I'm using to deserialize - not with the way I am serializing the
DataSet?

How should the DataSet be deserialized?
Re: newbie: How to use WriteXmlSchema? deko
2/24/2006 8:42:19 AM
[quoted text, click to view]

Re: newbie: How to use WriteXmlSchema? Martin Honnen
2/24/2006 3:46:57 PM


[quoted text, click to view]


[quoted text, click to view]

What is xmlFile? If that is a string with the XML markup then it should
work. If that is a string with a file name then StringReader is not the
right tool.

--

Martin Honnen --- MVP XML
Re: newbie: How to use WriteXmlSchema? Martin Honnen
2/24/2006 5:29:38 PM


[quoted text, click to view]


[quoted text, click to view]

But a StringReader does not read from a file, if you want to read from a
file then simply use e.g.
projectData.ReadXml(xmlFile)

--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button