Serialization help needed

  • Thread starter Thread starter Jens Weiermann
  • Start date Start date
J

Jens Weiermann

I'm writing kind of a "network monitor" application in .NET 2.0 (C#); At
runtime, the user should be able to configure all kinds of different
"sensors" and I want to serialize them to an xml file.

There are several different classes for different sensors, all derived from
an abstract base sensor class:

public class Sensor
{
public SensorResult Sense();
}

public class PingSensor: Sensor
{
string Hostname;
int timeout;
...
}

public class PortSensor: Sensor
{
string Hostname;
int Port;
...
}
....

Ideally, the resulting xml file would like this (for example):

<sensor type="Sensaction.PingSensor, sensaction.core"
name="Ping some server" host="SomeServer" timeout="5000"/>

<sensor type="Sensaction.PortSensor, sensaction.core"
name="Sql port on db server" host="dbserver" port="1433"/>

<sensor type="Sensaction.HttpSensor, sensaction.core"
name="Homepage availability" url="http://www.somedomain.com/index.htm"/>

I've seen the "Namespace.Class, Assembly" notation in several .config files
and like them pretty much (because some advanced users might want to create
these xml files by hand).
My questions are:
1. Is serialization the way to go, or am I better off with
System.Configuration.* ?
2. If it is serialization, how do I start? (I must admit I never did this
before)

Any help would be greatly appreciated!

Jens
 
Configuration file.

Use serialization to save the state of classes. Use configuration to set the
initial state of the app.
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
 
Kevin said:
Configuration file.

Use serialization to save the state of classes. Use configuration to set the
initial state of the app.

Ok, thanks for your answer. How would I start doing it with a configuration
file? I've looked up the help file, but anywhere it mentions
ConfigurationSection or similar classes, it does so in relation to ASP.NET,
so I'm unsure if this also applies to winforms...

Jens
 
I vote for c) all of the above.

The good thing about using the configuration file is that fact that you have
a single location for all of your configuration information.
When you read your configuration data from the CML file (via a custom
configuration section handler, you can get the XML content of that custom
section.
when you get that xml, you create an XmlReader with that content and you're
ready to serialize/deserialize your data.
 
David said:
I vote for c) all of the above.

The good thing about using the configuration file is that fact that you have
a single location for all of your configuration information.
When you read your configuration data from the CML file (via a custom
configuration section handler, you can get the XML content of that custom
section.
when you get that xml, you create an XmlReader with that content and you're
ready to serialize/deserialize your data.

Thanks for your answer - that sounds good! You don't happen to have an
example at hand, do you?

Jens
 
You can work this the same way as ASP.Net if you like, using an app.config
file, but an easier method is to use the Application Settings of the
WinForms project. Here is a detailed section about Application Settings:

http://msdn2.microsoft.com/en-us/library/0zszyc6e.aspx

Basically, the Application Settings in a WinForms project is a file under
the Properties folder (in Visual Studio), which is called "Settings." It has
a GUI for working with it in Visual Studio.

One of the advantages to using the Settings file is that Application
Settings can be scoped to Application, or to User. User Settings can be
changed (and saved) by the User during the use of the program, while
Application Settings are immutable. Once a Setting has been set, you refer
to it as (example):

client.ReceiveTimeout = Properties.Settings.Default.ReceiveTimeout;

You'll also notice that Application Settings are strongly typed.
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
 
Back
Top