473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Nov 16 '05 #1
3 1673
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.

Nov 16 '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)

Nov 16 '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)
Nov 16 '05 #4

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

Similar topics

3
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...
5
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
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
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...
2
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
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
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...
1
by: rmgalante | last post by:
I have a Windows Service that reads and writes an XML file to disk periodically. I use the XmlSerializer to serialize and deserialize the XML file on disk. I am writing the file using an...
2
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.