473,508 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Apparent bug in XmlSerializer or XmlTextWriter or something

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.
Jul 20 '05 #1
3 4149
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" <us****@fub-group.de> wrote in message
news:c5*************@news.t-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.

Jul 20 '05 #2
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)

Jul 20 '05 #3
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" <us****@fub-group.de> wrote in message
news:c6*************@news.t-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)
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5407
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
8
3542
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
0
9706
by: Riley Phelps | last post by:
When I use the XML Serializer in the following example, the first Serialize (with the class Pets) works, but the second (with the cplaa Pets2) fails. Which arrtubute nedds to be applied to make the...
3
1212
by: Integer Software | last post by:
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...
2
942
by: magister | last post by:
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different...
3
17714
by: grs | last post by:
I am using some Microsoft examples that: 1. Serialize an object using XmlSerializer and write a file out to the harddrive. 2. Read back in the file using XmlDocument.Load and populate a string. ...
3
10372
by: stax | last post by:
Hello, can somebody tell me how to serialize/deserialize a object containing a multi line string using the XmlSerializer class. One of the both windows linefeed chars get dumped somewhere down...
2
7567
by: Steve | last post by:
I'm an XML newb. I'm serializing a class and when I inspect the xml file, all the data is on one line rather than being nested and indented Is that normal? <code> StreamWriter sw = new...
4
6842
by: SteveT | last post by:
I have a data structure that I am writing to an XML file using XmlSerializer. XmlSerializer xSer = new XmlSerializer(typeof(MyData)); StreamWriter sw = new StreamWriter("SomeData.xml");...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.