xml serializer bug

G

Guest

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;
}
}
 
P

Patrick Steele [MVP]

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?
 
G

Guest

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
 
P

Patrick Steele [MVP]

Hi Patrick,


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.
 
G

Guest

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.
 
G

Guest

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();
}
 
G

Guest

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top