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

Can I fake a parent element in a serializable class?

Hello

I suspect the answer is going to be no, but...

I have a class that contains a property defined as an abstract class.
I would like to control the format of the generated XML to include a
container element that isn't actually implemented in my class.

This is best described by an example.

I have an abstract class that represents an expression:

public abstract class Expression {...}

I have extended this abstract class with various types of expression,
e.g.

public class Literal : Expression {...}
public class Field : Expression {...}
public class Operation : Expression {...}

I make use of this abstract class in another class, exposing it as a
property:

public class Comparison
{
private Expression _argument1;
private Expression _argument2;

public Expression Argument1
{
get {return _expression;}
set {_expression = value;}
}

public Expression Argument2
{
get {return _expression;}
set {_expression = value;}
}
}

To prepare the class for serialization, I decorate it using
[Serializable] and adding [XmlElement] attributes to the public
properties:

[Serializable]
public class Comparison
{
private Expression _argument1;
private Expression _argument2;

[XmlElement("literal", typeof(Literal))]
[XmlElement("field", typeof(Field))]
[XmlElement("operation", typeof(Operation))]
public Expression Argument1
{
get {return _expression;}
set {_expression = value;}
}

[XmlElement("literal", typeof(Literal))]
[XmlElement("field", typeof(Field))]
[XmlElement("operation", typeof(Operation))]
public Expression Argument2
{
get {return _expression;}
set {_expression = value;}
}
}

When argument 1 is a field and argument 2 is a literal (for example),
this generates XML of the form

<Comparison>
<field>
--field elements
</field>
<literal>
--literal elements
</literal>
</Comparison>

However, what I really want is XML of the form:

<Comparison>
<argument1>
<field>
--field elements
</field>
</argument1>
<argument2>
<literal>
--literal elements
</literal>
</argument2>
</Comparison>

So my question is: can this be achieved without physically
implementing a container class to represent the argument elements?

i.e. I don't want to have to change my class model by introducing a new
class.

Thanks very much

Sandy

Oct 27 '06 #1
0 1200

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

Similar topics

2
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the...
1
by: Duncan Aitken via .NET 247 | last post by:
All, In an inherited managed c++ class, how do you call the contstructor of the parent class? I tried with the usual Parent::Parent(...), but get compiler error C3257. Any ideas? Thanks, ...
1
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
9
by: Developer | last post by:
Hi, How can one tell wsdl.exe/VS.NET web service proxy generatioon to to put on imported classes? For example, i fin your web service you use a class: class Data { }
0
by: Matt S | 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...
2
by: Burgazon | last post by:
Hi, I encountered the following problem when trying to set control parent when the control reside in a different AppDomain. Here is the pseudo code descibing the problem: pay attention that...
0
by: sandycormie | last post by:
Hello I suspect the answer is going to be no, but... I have a class that contains a property defined as an abstract class. I would like to control the format of the generated XML to include a...
0
by: Ross Presser | last post by:
I want to deserialize this XML: <Header> <Sender> <Credential domain="DUNS"> <Identity>12345</Identity> <sharedSecret>coyote</sharedSecret> </Credential> <Credential domain="email">...
0
by: Guzeppi | last post by:
Hi, i'm using linq to load an xml structure into my classes. the xml consists of the same node nested for multiple levels e.g. <node id="node_id01" name="node 01"> <node id="node_id0101"...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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
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...

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.