Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2006

Tip: Looking for answers? Try searching our database.

Can I fake a parent element in a serializable class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sandycormie@gmail.com - 27 Oct 2006 22:49 GMT
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
Scott M. - 29 Oct 2006 23:31 GMT
Ok, your message is posted already!

> Hello
>
[quoted text clipped - 103 lines]
>
> Sandy

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.