473,385 Members | 1,427 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,385 software developers and data experts.

xml serializer bug

Hi,

I use this class to save application settings in a xml file. Sometime I have
exception error in the Load method, and sometime in the Save method. Is this
a bug in NET or is there something I do wrong ? I load in the form Load and
save in the FormClosing. This is the class:

public class Config
{
public formSettings MainForm = new formSettings();
public appVersion AppVersion = new appVersion();
public Server server = new Server();

public Config Load(string FileName)
{
try {
XmlSerializer serializer = new XmlSerializer(typeof(Config));
FileStream stream = new FileStream(FileName, FileMode.Open);
try {
return (Config)serializer.Deserialize(stream);
} finally {
stream.Close();
}
} catch {
return new Config();
}
}

public void Save(string FileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(Config));
TextWriter writer = new StreamWriter(FileName);
serializer.Serialize(writer, this);
writer.Close();
}

public class formSettings
{
public Point Location;
public Size Size;
}

public class Server
{
public string server = "localhost";
public string port = "2804";
public bool activate = false;
}

public class appVersion
{
public string version;
}
}

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 17 '05 #1
6 4635
In article <9B**********************************@microsoft.co m>,
Wi**************@discussions.microsoft.com says...
I use this class to save application settings in a xml file. Sometime I have
exception error in the Load method, and sometime in the Save method. Is this
a bug in NET or is there something I do wrong ? I load in the form Load and
save in the FormClosing.


I've used that basic approach many times. The only difference is that I
make the Load method static so I don't need to create Config class just
to load one.

What exception are you getting?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 17 '05 #2
Hi Patrick,
What exception are you getting?


when I remove the exception block, on the line:
return (Config)serializer.Deserialize(stream);
the exception is:
- when casting from a number, the value must be a number less than infinity.

When I put the exception block back, then it sometime has exception on
saving. But this one say:
- there was an error generating the xml document (if I then check the
document it is completely empty).

Note that this is not every time, that is what make it very wierd. Also stop
debugging and start again then almost allways it is working.

rgds, Wilfried
Nov 17 '05 #3
In article <09**********************************@microsoft.co m>,
Wi**************@discussions.microsoft.com says...
Hi Patrick,
What exception are you getting?


when I remove the exception block, on the line:
return (Config)serializer.Deserialize(stream);
the exception is:
- when casting from a number, the value must be a number less than infinity.

When I put the exception block back, then it sometime has exception on
saving. But this one say:
- there was an error generating the xml document (if I then check the
document it is completely empty).

Note that this is not every time, that is what make it very wierd. Also stop
debugging and start again then almost allways it is working.


Sorry -- not sure what to say. It's wierd that it only does it
occasionally. At the time you get the exception, have you looked at the
values it's serializing? Perhaps one of the is a very wierd value.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 17 '05 #4
Hi Patrick,
Sorry -- not sure what to say. It's wierd that it only does it
occasionally. At the time you get the exception, have you looked at the
values it's serializing? Perhaps one of the is a very wierd value.


Will check it out the next time I get the exception. Mean while I will
change my Load method static as you do. Seems better because now I have to
create the class first to deserialize it. Maybe this has something to do
with it. I will let you know what happens.
Nov 17 '05 #5
I just had the error again in another project. The Load method is here
outside the class. I checked the sml file and there is no strange value in
it. It is only size and position of main form. I copy it here, including the
way I load the file and the class itself. This is so basic, wy the error,
and wy only once a while ?

The cast error is in the line: "cfg =
(Config)serializer.Deserialize(stream);" I beginning to think itis a bug in
NET, but then again must be more people with that problem ?

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MainForm>
<Location>
<X>415</X>
<Y>361</Y>
</Location>
<Size>
<Width>827</Width>
<Height>541</Height>
</Size>
</MainForm>
<AppVersion>
<version>1.0.0.0</version>
</AppVersion>
</Config>

The class in this project is only this:

public class Config
{
public formSettings MainForm = new formSettings();
public appVersion AppVersion = new appVersion();

public void Save(string FileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(Config));
TextWriter writer = new StreamWriter(FileName);
serializer.Serialize(writer, this);
writer.Close();
}

public class formSettings
{
public Point Location;
public Size Size;
}

public class appVersion
{
public string version;
}
}

And the way I load is as this:

private void Form1_Load(object sender, EventArgs e)
{
string AppPath =
Path.GetDirectoryName(Application.ExecutablePath);
main_configFile = Path.Combine(AppPath, "main_cfg.xml");
if (File.Exists(dock_configFile))
dockPanel.LoadFromXml(dock_configFile,
deserializeDockContent);
if (File.Exists(main_configFile)) {
XmlSerializer serializer = new XmlSerializer(typeof(Config));
FileStream stream = new FileStream(main_configFile,
FileMode.Open);
try {
cfg = (Config)serializer.Deserialize(stream);
} finally {
stream.Close();
}
Location = cfg.MainForm.Location;
Size = cfg.MainForm.Size;
} else
cfg = new Config();
}

Nov 17 '05 #6
I have find following. once a while I get when starting an application this
popup window:
one or more projects in the solution do not contain user code and cannot be
debugged with 'just my code' setting enabled, etc.. to suppress this message,
disable 'warn if no user code on launch' in the debugger option page.

When I have that message I get the exception. This is already 3 time
togethere so not coincidance. Wy this message is only once a while ? I
checked xml file at this point and it is all ok. hre it is:

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MainForm>
<Location>
<X>924</X>
<Y>415</Y>
</Location>
</MainForm>
<AppVersion>
<version>1.0.0.0</version>
</AppVersion>
<server>
<nic>0.0.0.0</nic>
<port>2804</port>
<activateSrv>true</activateSrv>
</server>
</Config>
Nov 17 '05 #7

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

Similar topics

0
by: CB | last post by:
In the 2.5 distribution there is a sample called DOM3. It imports a package org.w3c.dom.bootstrap.DOMImplementationRegistry. Where can I find the bootstrap package? Using XMLSerializer ...
0
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
1
by: James | last post by:
Hi, I have an array of structures I would like to turn into an XML string using the XML serializer. The problem I am running into is that I get an exception. Depending on what data type I set...
3
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
0
by: Albert Jan | last post by:
Hi, I use the XmlSerializer in C# to serialize an object containing an email message to xml. This works often fine, but for some mailmessages the serialization fails with the message 'There was...
1
by: Michael A. Covington | last post by:
Is there an easy way to insert an encryption routine in between the XML serializer and the file it is writing or reading?
6
by: Dave Veeneman | last post by:
Is XML Serialization worth using to persist classes to file? I've been trying for a day to get it to work on some fairly simple classes. what I'm getting back are error messages like "There is an...
8
by: Q. John Chen | last post by:
I have following code: public class Member { public string FirstName; public string LastName; public string GetXml() { StringBuilder sb = new StringBuilder(); XmlSerializer serializer
1
by: hawkeye.parker | last post by:
using .net 1.1 i think i might want to replace the xml serializer which my .Net webservice uses with a custom serializer. anyone know: a) is this possible? b) if so, is it terribly difficult...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.