473,606 Members | 2,200 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.D eserialize gives a System.InvalidO perationExcepti on 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"?><setting s
xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"><Inst allDir>G:\Visua l
Studio Projects\Initio 2_take2\bin\Deb ug</InstallDir><Dat aDir>G:\Visual
Studio Projects\Initio 2_take2\bin\Deb ug</DataDir></settings>

After shortening the DataDir path, and rewriting the Xml file, it looks
like this:

ÿþ<?xml version="1.0" encoding="utf-16"?><setting s
xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"><Inst allDir>G:\Visua l
Studio
Projects\Initio 2_take2\bin\Deb ug</InstallDir><Dat aDir>\\Linux\pu blic\temp</DataDir></settings>bin\De bug</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 writeOutConfigS tore()
{
try
{
IsolatedStorage File isoStore =
IsolatedStorage File.GetStore(I solatedStorageS cope.User |
IsolatedStorage Scope.Assembly, null, null);
IsolatedStorage FileStream target =
new
IsolatedStorage FileStream("Ini tio",
FileMode.OpenOr Create,
FileAccess.Writ e,
FileShare.Write ,
10240,
isoStore);
XmlSerializer serializer =
new
XmlSerializer(t ypeof(settings) );
XmlWriter writer =
new XmlTextWriter(t arget,
Encoding.Unicod e);
// Serialize using the XmlTextWriter.
serializer.Seri alize(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
{
IsolatedStorage File isoStore =
IsolatedStorage File.GetStore(I solatedStorageS cope.User |
IsolatedStorage Scope.Assembly, null, null);

IsolatedStorage FileStream isoStream =
new IsolatedStorage FileStream(
"Initio",
FileMode.Open,
FileAccess.Read ,
FileShare.Read,
isoStore);
XmlSerializer serializer = new XmlSerializer(t ypeof(settings) );
XmlReader reader = new
XmlTextReader(i soStream); // This should probably be a xml reader or
something
ourSettings = (settings)
serializer.Dese rialize(reader) ;
// Read the data.

reader.Close();
isoStream.Close ();
isoStore.Close( );
modified = false;
return;
}
catch(System.IO .FileNotFoundEx ception)
{
// This means first time user, and
needs to build a default settings file.
MessageBox.Show ("New User");
createNewConfig Store();
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 4182
Try writing out the file using FileMode.Open instead of
FileMode.OpenOr Create.

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.D eserialize gives a System.InvalidO perationExcepti on 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"?><setting s
xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"><Inst allDir>G:\Visua l Studio Projects\Initio 2_take2\bin\Deb ug</InstallDir><Dat aDir>G:\Visual
Studio Projects\Initio 2_take2\bin\Deb ug</DataDir></settings>

After shortening the DataDir path, and rewriting the Xml file, it looks
like this:

ÿþ<?xml version="1.0" encoding="utf-16"?><setting s
xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"><Inst allDir>G:\Visua l Studio
Projects\Initio 2_take2\bin\Deb ug</InstallDir><Dat aDir>\\Linux\pu blic\temp</D
ataDir></settings>bin\De bug</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 writeOutConfigS tore()
{
try
{
IsolatedStorage File isoStore =
IsolatedStorage File.GetStore(I solatedStorageS cope.User |
IsolatedStorage Scope.Assembly, null, null);
IsolatedStorage FileStream target =
new
IsolatedStorage FileStream("Ini tio",
FileMode.OpenOr Create, , FileShare.Write ,
10240,
isoStore);
XmlSerializer serializer =
new
XmlSerializer(t ypeof(settings) );
XmlWriter writer =
new XmlTextWriter(t arget,
Encoding.Unicod e);
// Serialize using the XmlTextWriter.
serializer.Seri alize(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
{
IsolatedStorage File isoStore =
IsolatedStorage File.GetStore(I solatedStorageS cope.User |
IsolatedStorage Scope.Assembly, null, null);

IsolatedStorage FileStream isoStream =
new IsolatedStorage FileStream(
"Initio",
FileMode.Open,
FileAccess.Read ,
FileShare.Read,
isoStore);
XmlSerializer serializer = new XmlSerializer(t ypeof(settings) );
XmlReader reader = new
XmlTextReader(i soStream); // This should probably be a xml reader or
something
ourSettings = (settings)
serializer.Dese rialize(reader) ;
// Read the data.

reader.Close();
isoStream.Close ();
isoStore.Close( );
modified = false;
return;
}
catch(System.IO .FileNotFoundEx ception)
{
// This means first time user, and
needs to build a default settings file.
MessageBox.Show ("New User");
createNewConfig Store();
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.OpenOr Create 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.OpenOr Create.

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.OpenOr Create 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.OpenOr Create.

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
5412
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 create duplicate XML representations of the shared object, but instead use IDREFs to refer to the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <?xml version="1.0" encoding="utf-8"?> <MyRootClass...
8
3557
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 initially tested my design by building a collection in code and then serializing it to/from an XML file, which worked fine. However, I have hit a brick wall trying to restore the data from SQL Server. I originally had my collection and object
0
9709
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 second Serialize work namespace SerializeTes public class Class args Test _test = new Test() _test.TestAnimal()
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 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...
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 attributes, all share a base set of 4, one of the question types can have children with <option> elements, this is how the xml looks after serialization.... If you notice there is an extra <SelectionList> around the <option>'s in the final...
3
17718
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. What I would like to do is use MemoryStream to do this instead of writing to the harddrive, but I am lost in the readers, writers etc. Can someone show me how to to this using a memory stream. thanks
3
10398
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 the road. It would be awesome somebody could answer this, sadly most of my questions don't get answered. Regards,
2
7581
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 StreamWriter(m_optionsXmlFilePath, false); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Options)); XmlTextWriter xmlTextWriter = new XmlTextWriter(sw);
4
6854
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"); xSer.Serialize(sw, theData); sw.Close(); I need to add a style reference within the file that looks like this: <?xml-stylesheet type="text/xsl" href="MyStyle.xsl"?>
0
8439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8430
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8094
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5966
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
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 we have to send another system
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.