I
Integer Software
Hi.
I have a really simple set of classes that writes 2 pathnames to a xml
file. I can write the default ok. Then if I change 1 pathname to a
shorter one, then rewrite the xml file, the remains of the old pathname,
and closing tags are left on the end resulting in an invalid XML file.
XmlSerializer.Deserialize gives a System.InvalidOperationException with
additional information: Error in the XML document.
My default file for example looks like: (sorry about the wrapping if its
wrapped)
ÿþ<?xml version="1.0" encoding="utf-16"?><settings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><InstallDir>G:\Visual
Studio Projects\Initio2_take2\bin\Debug</InstallDir><DataDir>G:\Visual
Studio Projects\Initio2_take2\bin\Debug</DataDir></settings>
After shortening the DataDir path, and rewriting the Xml file, it looks
like this:
ÿþ<?xml version="1.0" encoding="utf-16"?><settings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><InstallDir>G:\Visual
Studio
Projects\Initio2_take2\bin\Debug</InstallDir><DataDir>\\Linux\public\temp</DataDir></settings>bin\Debug</DataDir></settings>
See at the end, the "bin\Debug</DataDir></settings>" bit at the end
should not be there.
Heres my relevant code: Writing section:
public void writeOutConfigStore()
{
try
{
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream target =
new
IsolatedStorageFileStream("Initio",
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.Write,
10240,
isoStore);
XmlSerializer serializer =
new
XmlSerializer(typeof(settings));
XmlWriter writer =
new XmlTextWriter(target,
Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, ourSettings);
writer.Close();
target.Close();
isoStore.Close();
modified = false; }
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
My reading section:
public config()
{
//
// TODO: Add constructor logic here
//
ourSettings = new settings();
// Check for per user Isolated Storage
try
{
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(
"Initio",
FileMode.Open,
FileAccess.Read,
FileShare.Read,
isoStore);
XmlSerializer serializer = new XmlSerializer(typeof(settings));
XmlReader reader = new
XmlTextReader(isoStream); // This should probably be a xml reader or
something
ourSettings = (settings)
serializer.Deserialize(reader);
// Read the data.
reader.Close();
isoStream.Close();
isoStore.Close();
modified = false;
return;
}
catch(System.IO.FileNotFoundException)
{
// This means first time user, and
needs to build a default settings file.
MessageBox.Show("New User");
createNewConfigStore();
return;
}
}
Any hints, pointers, or working code would be appreciated.
Thanks
Kurt Häusler
MSDN Universal Customer if it helps.
I have a really simple set of classes that writes 2 pathnames to a xml
file. I can write the default ok. Then if I change 1 pathname to a
shorter one, then rewrite the xml file, the remains of the old pathname,
and closing tags are left on the end resulting in an invalid XML file.
XmlSerializer.Deserialize gives a System.InvalidOperationException with
additional information: Error in the XML document.
My default file for example looks like: (sorry about the wrapping if its
wrapped)
ÿþ<?xml version="1.0" encoding="utf-16"?><settings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><InstallDir>G:\Visual
Studio Projects\Initio2_take2\bin\Debug</InstallDir><DataDir>G:\Visual
Studio Projects\Initio2_take2\bin\Debug</DataDir></settings>
After shortening the DataDir path, and rewriting the Xml file, it looks
like this:
ÿþ<?xml version="1.0" encoding="utf-16"?><settings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><InstallDir>G:\Visual
Studio
Projects\Initio2_take2\bin\Debug</InstallDir><DataDir>\\Linux\public\temp</DataDir></settings>bin\Debug</DataDir></settings>
See at the end, the "bin\Debug</DataDir></settings>" bit at the end
should not be there.
Heres my relevant code: Writing section:
public void writeOutConfigStore()
{
try
{
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream target =
new
IsolatedStorageFileStream("Initio",
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.Write,
10240,
isoStore);
XmlSerializer serializer =
new
XmlSerializer(typeof(settings));
XmlWriter writer =
new XmlTextWriter(target,
Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, ourSettings);
writer.Close();
target.Close();
isoStore.Close();
modified = false; }
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
My reading section:
public config()
{
//
// TODO: Add constructor logic here
//
ourSettings = new settings();
// Check for per user Isolated Storage
try
{
IsolatedStorageFile isoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(
"Initio",
FileMode.Open,
FileAccess.Read,
FileShare.Read,
isoStore);
XmlSerializer serializer = new XmlSerializer(typeof(settings));
XmlReader reader = new
XmlTextReader(isoStream); // This should probably be a xml reader or
something
ourSettings = (settings)
serializer.Deserialize(reader);
// Read the data.
reader.Close();
isoStream.Close();
isoStore.Close();
modified = false;
return;
}
catch(System.IO.FileNotFoundException)
{
// This means first time user, and
needs to build a default settings file.
MessageBox.Show("New User");
createNewConfigStore();
return;
}
}
Any hints, pointers, or working code would be appreciated.
Thanks
Kurt Häusler
MSDN Universal Customer if it helps.