473,782 Members | 2,419 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.De serialize( tr );
tr.Close();
} catch (System.IO.File NotFoundExcepti on) {
// 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 2577
"dmonder" <dm*****@gmail. comwrote in message
news:11******** *************@n 59g2000hsh.goog legroups.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(TextReade r tr = new StreamReader( "config.xml " ))
{
config = (Config)xmls.De serialize( tr );
}
} catch (System.IO.File NotFoundExcepti on) {
// How do I set up the defaults??
config = new Config();
config.Property 1 = defaultForPrope rty1;
// etc.
}
--
John Saunders [MVP]
May 4 '07 #2

} catch (System.IO.File NotFoundExcepti on) {
// 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.variable s = 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.De serialize( tr );
tr.Close();
} catch (System.IO.File NotFoundExcepti on) {
// 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
5430
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 create duplicate XML representations of the shared object, but instead use IDREFs to refer to the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <?xml version="1.0" encoding="utf-8"?> <MyRootClass...
1
4323
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 FileMessage or ChatMessage. So how to get that object and use it I have written following code for serialization public void Send(Message message) { NetworkStream netWorkStream=null;
3
7003
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 XmlSerializer will only serialize the collection items - with the default root as ArrayofMyItems etc. My custom collection class has some additional public properties that I would like to include in the serialization above the items element array (in
4
11402
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 should have worked fine since the class in question was already being serialized and deserialized as part of a Web service interface. What I found was that by deserializing from an XmlNodeReader instead of an XmlTextReader, XML Serialization doesn't work...
3
4517
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 XmlText. In my main class, i have created an arraylist which contains a collection of this class FormField. Basically its: public void Add( string sName, string sValue )
12
8502
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. I, too, am getting nasty FileNotFound exceptions. I've read, and digested the article, and I think I've found a bug -- it's difficult to track, though it does happen often.
4
1992
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 working fine but when I try to instantiate an XmlSerializer object with: Dim xmls As New XmlSerializer(GetType(CLayerDefinition)) I get the following error:
0
2309
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 some digging, I found XmlSerializer chokes on 0x03 chars. It probably chokes on many others, but this one I found. It serializes ok, but chokes on deserialize on "<Field1>&#x3;</Field1>". So the questions are: 1) Why does serializer produce...
2
1730
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( "config.xml" ); config = (Config)xmls.Deserialize( tr ); tr.Close(); } catch (System.IO.FileNotFoundException) {
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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 we have to send another system

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.