Apparent bug in XmlSerializer or XmlTextWriter or something

Integer Software

Usenet Archive
Joined
Apr 16, 2004
Messages
2
Reaction score
0
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.
 

Tejal Joshi (MSFT )

Usenet Archive
Joined
Apr 16, 2004
Messages
2
Reaction score
0
Try writing out the file using FileMode.Open instead of FileMode.OpenOrCreate.

The first time this command executes (since there is no pre-exsiting file) it creates the file. Next time however, it opens the already existing file and overwrites content starting from the beginning of the file. Resulting in any TRAILING content remaining as is (which is the behavior you see here)
Integer Software said:
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"> said:
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"
 

Integer Software

Usenet Archive
Joined
Apr 16, 2004
Messages
2
Reaction score
0
Hi Tejal.

Changing FileMode.OpenOrCreate to FileMode.Open does not fix the problem. The wierd behaviour of leaving trailing content remains.

I have never seen this anywhere in programming, by opening a file and rewriting it, I have NEVER seen trailing content remaining except where its deliberatly programmed. And it doesnt make any sense at all in a pre-programmed function like the XML writing functions. I mean surely an EOF is written in any case right?

I believe this can only be a bug in the functions, and I will attempt to work around it by deleting the file first.

Thanks anyway.
 

Tejal Joshi (MSFT )

Usenet Archive
Joined
Apr 16, 2004
Messages
2
Reaction score
0
oops I was typing too fast - I meant use FileMode.Create. Obviously FileMode.Open would only reopen the existing file and give the same old behavior!

The documentation for the FileMode enumeration is avialable http://msdn.microsoft.com/library/d...pref/html/frlrfsystemiofilemodeclasstopic.asp

Integer Software said:
Hi Tejal.

Changing FileMode.OpenOrCreate to FileMode.Open does not fix the problem. The wierd behaviour of leaving trailing content remains.

I have never seen this anywhere in programming, by opening a file and rewriting it, I have NEVER seen trailing content remaining except where its deliberatly programmed. And it doesnt make any sense at all in a pre-programmed function like the XML writing functions. I mean surely an EOF is written in any case right?

I believe this can only be a bug in the functions, and I will attempt to work around it by deleting the file first.

Thanks anyway.
here)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top