473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer and defaults

I am using the XMLSerializer to map the XML below to an object as
follows:

Config config
XmlSerializer xmls = new XmlSerializer( typeof( Config ) );
try {
TextReader tr = new StreamReader( "config.xml" );
config = (Config)xmls.Deserialize( tr );
tr.Close();
} catch (System.IO.FileNotFoundException) {
// How do I set up the defaults??
}

My problem is that I want to be able set my config object to defaults
if the xml file is not found. Any ideas?

David

----- XML File -----

<?xml version="1.0"?>
<Config>
<Directories>
<Directory Name="Cleaned" Location="" />
<Directory Name="Received" Location="" />
</Directories>
<Extention Default="csv" />
<Files>
<File Name="Cleaned" Location="" />
<File Name="Received" Location="" />
</Files>
<FileHistory>
<FileName></FileName>
</FileHistory>
<Filters Sorted="true">
<Filter>
<Name>All files (*.*)</Name>
<Data>*.*</Data>
</Filter>
<Filter>
<Name>Comma Separated Files (*.csv)</Name>
<Data>*.csv</Data>
<Selected/>
</Filter>
</Filters>
</Config>

May 4 '07 #1
2 1717
"dmonder" <dm*****@gmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...

My answers are inline:
>I am using the XMLSerializer to map the XML below to an object as
follows:

Config config
XmlSerializer xmls = new XmlSerializer( typeof( Config ) );
try {
using(TextReader tr = new StreamReader( "config.xml" ))
{
config = (Config)xmls.Deserialize( tr );
}
} catch (System.IO.FileNotFoundException) {
// How do I set up the defaults??
config = new Config();
config.Property1 = defaultForProperty1;
// etc.
}
--
John Saunders [MVP]
May 4 '07 #2

} catch (System.IO.FileNotFoundException) {
// How do I set up the defaults??

For example :
You can read from default xml from resources.
or
You can directly set the default values
config = new Config();
confic.variables = defaultValue;
}
or define Config with pre-set default values.

Alex
http://devkids.blogspot.com
I am using the XMLSerializer to map the XML below to an object as
follows:

Config config
XmlSerializer xmls = new XmlSerializer( typeof( Config ) );
try {
TextReader tr = new StreamReader( "config.xml" );
config = (Config)xmls.Deserialize( tr );
tr.Close();
} catch (System.IO.FileNotFoundException) {
// How do I set up the defaults??
}
My problem is that I want to be able set my config object to defaults
if the xml file is not found. Any ideas?

David

----- XML File -----
><?xml version="1.0"?>
<Config>
<Directories>
<Directory Name="Cleaned" Location="" />
<Directory Name="Received" Location="" />
</Directories>
<Extention Default="csv" />
<Files>
<File Name="Cleaned" Location="" />
<File Name="Received" Location="" />
</Files>
<FileHistory>
<FileName></FileName>
</FileHistory>
<Filters Sorted="true">
<Filter>
<Name>All files (*.*)</Name>
<Data>*.*</Data>
</Filter>
<Filter>
<Name>Comma Separated Files (*.csv)</Name>
<Data>*.csv</Data>
<Selected/>
</Filter>
</Filters>
</Config>

May 4 '07 #3

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

Similar topics

5
5407
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...
1
4299
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting...
3
6987
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the...
4
11349
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
3
4487
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as...
12
8450
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
1969
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
0
2269
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
2
2558
by: dmonder | last post by:
I am using the XMLSerializer to map the XML below to an object as follows: Config config XmlSerializer xmls = new XmlSerializer( typeof( Config ) ); try { TextReader tr = new StreamReader(...
3
3291
by: kimtherkelsen | last post by:
Hi, I want to send XML data from a server to some clients over a network connection using the TCP/IP protocol. If I send the XMLs as byte arrays I need to insert header information in the data to...
0
7095
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
7294
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,...
0
7361
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...
0
7470
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...
1
5026
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...
0
4693
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...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
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 ...
0
403
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...

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.