Apparent bug in XmlSerializer or XmlTextWriter or something

Discussion in 'XML' started by Integer Software, Apr 16, 2004.

  1. 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.
    Integer Software, Apr 16, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:c5on75$5ph$07$-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
    > 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</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.
    Tejal Joshi \(MSFT\), Apr 16, 2004
    #2
    1. Advertising

  3. 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)
    Integer Software, Apr 19, 2004
    #3
  4. 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

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

    "Integer Software" <> wrote in message
    news:c606gr$14a$01$-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)
    Tejal Joshi \(MSFT\), Apr 19, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bengt Richter
    Replies:
    3
    Views:
    281
    Steve Holden
    Jan 19, 2005
  2. Harold Yarmouth

    Apparent bug in FileLock

    Harold Yarmouth, Nov 19, 2008, in forum: Java
    Replies:
    1
    Views:
    325
    Harold Yarmouth
    Nov 20, 2008
  3. Bob Rock
    Replies:
    3
    Views:
    159
    Ming Chen [.NET MVP]
    Jun 17, 2004
  4. Bill Kelly
    Replies:
    6
    Views:
    335
    Bill Kelly
    Aug 27, 2004
  5. Eric J. Roode

    Apparent bug in 5.8 wrt tied scalars

    Eric J. Roode, Nov 19, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    105
    Ilya Zakharevich
    Nov 23, 2005
Loading...

Share This Page