473,591 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("lit eral", typeof(Literal) )]
[XmlElement("fie ld", typeof(Field))]
[XmlElement("ope ration", typeof(Operatio n))]
public Expression Argument1
{
get {return _expression;}
set {_expression = value;}
}

[XmlElement("lit eral", typeof(Literal) )]
[XmlElement("fie ld", typeof(Field))]
[XmlElement("ope ration", typeof(Operatio n))]
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 1789
Ok, your message is posted already!

<sa*********@gm ail.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.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("lit eral", typeof(Literal) )]
[XmlElement("fie ld", typeof(Field))]
[XmlElement("ope ration", typeof(Operatio n))]
public Expression Argument1
{
get {return _expression;}
set {_expression = value;}
}

[XmlElement("lit eral", typeof(Literal) )]
[XmlElement("fie ld", typeof(Field))]
[XmlElement("ope ration", typeof(Operatio n))]
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
5297
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 pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a...
1
1446
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, Duncan. -------------------------------- From: Duncan Aitken
1
1324
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 from ArrayList public class Fields: ArrayList
9
3813
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
4075
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 using complex type structures declared in the WSDL file.
2
3487
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 both controls inherits from UserControl which inherites (indirectly)from MarshalByRefObject.
0
1213
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 container element that isn't actually implemented in my class. This is best described by an example.
0
381
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 container element that isn't actually implemented in my class. This is best described by an example.
0
2042
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"> <Identity>rpresser@foobar.com</Identity>
0
2578
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" name="node 01 01"> <node id="node_id010101" name="node 01 01 01"> <node id="node_id01010101" name="node 01 01 01 01"> <node id="node_id0101010101" name="node 01 01 01 01 01">
0
7934
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
8362
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...
1
7992
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
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
6639
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
5732
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
5400
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();...
1
2378
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
0
1199
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.