Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Apparent bug in XmlSerializer or XmlTextWriter or something

Reply
Thread Tools

Apparent bug in XmlSerializer or XmlTextWriter or something

 
 
Integer Software
Guest
Posts: n/a
 
      04-16-2004
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
xmlnssd="http://www.w3.org/2001/XMLSchema"
xmlnssi="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
xmlnssd="http://www.w3.org/2001/XMLSchema"
xmlnssi="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.
 
Reply With Quote
 
 
 
 
Tejal Joshi \(MSFT\)
Guest
Posts: n/a
 
      04-16-2004
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)
--
This posting is provided "AS IS" with no warranties, and confers no rights.
..


--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Integer Software" <(E-Mail Removed)> wrote in message
news:c5on75$5ph$07$(E-Mail Removed)-online.com...
> 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
> xmlnssd="http://www.w3.org/2001/XMLSchema"
>

xmlnssi="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
> xmlnssd="http://www.w3.org/2001/XMLSchema"
>

xmlnssi="http://www.w3.org/2001/XMLSchema-instance"><InstallDir>G:\Visual
> Studio
>

Projects\Initio2_take2\bin\Debug</InstallDir><DataDir>\\Linux\public\temp</D
ataDir></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,

,
> 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 =

}
> 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.



 
Reply With Quote
 
 
 
 
Integer Software
Guest
Posts: n/a
 
      04-19-2004
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) wrote:
> 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)

 
Reply With Quote
 
Tejal Joshi \(MSFT\)
Guest
Posts: n/a
 
      04-19-2004
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/de...classtopic.asp

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Integer Software" <(E-Mail Removed)> wrote in message
news:c606gr$14a$01$(E-Mail Removed)-online.com...
> 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) wrote:
> > 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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apparent bug in FileLock Harold Yarmouth Java 1 11-20-2008 02:46 AM
Apparent bug in 5.8 wrt tied scalars Eric J. Roode Perl Misc 2 11-23-2005 07:40 AM
Has apparent 2.4b1 bug been fixed? flatten in Lib\compiler\ast.py overloads 'list' name Bengt Richter Python 3 01-19-2005 05:17 PM
[BUG?] apparent hang (win32 and Linux), have stack trace Bill Kelly Ruby 6 08-27-2004 02:11 AM
Serialization with XmlSerializer: how to set the XML root node to something different from <ArrayOfClassname>???? Bob Rock ASP .Net Web Services 3 06-17-2004 04:50 PM



Advertisments