473,486 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Xml Serialising Exposes a Class Too Much

Hi,

The code below is something I found on the net which serializes lists
of objects. I have a few problems with it though. First, I hate that
you have to have a default constructor - public Item() {}. For
example, if you have a Directory class I think it doesn't make sense
to have a default constructor. Instead, you wan't to pass it a string
etc. Whats a directory without a name? My point is that I think there
are situations where a class should not have a default constructor,
but if your serializing it your forced to anyway.

Also, in the example below, name and price are public. But if you make
them private then you are forced to define a get and set accessor for
each, otherwise xml serialization will get upset. This defeats the
whole point in having private with respect to the fact that users can
now set the name or value whenever they feel like it, rather than at
construction. So again with our Directory example, I'd prefer to have
no set on the Directory name since I think that this should only be
set when you create the object.

Direcory whatsMyPurpose = new Directory();
whatsMyPurpose.Name = ("c:/now_i_know");
whatsMyPurpose.Name = ("c:/or_do_i");

What do you think?

/Aine

using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// Shopping list class which will be serialized
[XmlRoot("shoppingList")]
public class ShoppingList {
private ArrayList listShopping;

public ShoppingList() {
listShopping = new ArrayList();
}

[XmlElement("item")]
public Item[] Items {
get {
Item[] items = new Item[ listShopping.Count ];
listShopping.CopyTo( items );
return items;
}
set {
if( value == null ) return;
Item[] items = (Item[])value;
listShopping.Clear();
foreach( Item item in items )
listShopping.Add( item );
}
}

public int AddItem( Item item ) {
return listShopping.Add( item );
}
}

// Items in the shopping list
public class Item {
[XmlAttribute("name")] public string name;
[XmlAttribute("price")] public double price;

public Item() {
}

public Item( string Name, string Price ) {
name = Name;
price = Price;
}
}

Oct 25 '07 #1
1 1197
If you use DataContractSerializer, you can mark private members
(including fields) as DataMember (not just public properties), however
I believe that you still need a default ctor. One other option is to
implement IXmlSerializable, but this largely defeats the purpose of an
automatic serializer...

Marc
Oct 25 '07 #2

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

Similar topics

15
2739
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
0
857
by: Craig | last post by:
I am implementing a C# class that exposes a COM interface so that a huge body of code that pre-dates .NET can use it. I noticed if I put a simple constructor in the class the class does not get...
1
1014
by: Stu | last post by:
Hi, I have a structure which contains an array of structures and I would like to be able to serialise/deserialise this Any ideas Tia Stu
0
1290
by: GAURAV KRISHNA | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element using...
0
904
by: Phill W. | last post by:
(Using VB'2005) I'm trying to serialise a DataSet that contains a "table" that is a Class derived from DataTable. <Serializable()_ Class CustomDataTable Inherits DataTable . . . I create...
10
1480
by: ssbae | last post by:
Hi~ I have two .cs files: Class1.cs namespace ClassLibrary1 { public class Class1 {
20
4001
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
1125
by: dans | last post by:
I want to serialise an array of structs into a MemoryStream so I can pass the contents in a message. I have written something that will Serialize and Deserialize in individual object. Is there a...
0
7094
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,...
0
6964
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
7123
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
7173
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...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
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...
0
5427
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,...
0
3066
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
259
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.