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

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 2551
"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
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
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
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
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
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
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
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
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
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(...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.