473,401 Members | 2,068 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,401 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
1 1763
Ok, your message is posted already!

<sa*********@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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 29 '06 #2

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: 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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...

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.