By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Creating a strongly typed object from a basic serialized class

P: n/a
Hi,

I am trying to implement some functionality as seen in MS CRM 3.0 whereby a
basic Xml is deserialized into an object which contains properties. What I
want to do from here is; cast the basic object into a more strongly typed
class which knows what properties to expect.

The basic top level class is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections;

namespace MessageOutgoing
{
[XmlRoot(Namespace="")]
/// <summary>
/// Describes the Message object.
/// </summary>
public class Message
{
private MessageType _type;
private MessageSubType _subType;
private ArrayList properties = new ArrayList();

/// <summary>
/// Gets or sets the MessageType enumeration.
/// </summary>
[XmlAttribute("Type")]
public MessageType MessageType
{
get
{
return _type;
}
set
{
_type = value;
}
}

/// <summary>
/// Gets or sets the type of message ie: ID01 - Reset request.
/// </summary>
[XmlAttribute("SubType")]
public MessageSubType MessageSubType
{
get
{
return _subType;
}
set
{
_subType = value;
}
}

/// <summary>
/// Gets or sets the properties collection. A properties collection
/// is a collection of property objects that make up a record.
/// </summary>
[XmlElement("Properties")]
public Properties[] Properties
{
get
{
Properties[] propertiesObj = new Properties[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;

Properties[] propertiesObj = (Properties[])value;
properties.Clear();
foreach (Properties property in propertiesObj)
properties.Add(property);
}
}
}

/// <summary>
/// Describes a collection of property objects.
/// </summary>
public class Properties
{
private ArrayList properties = new ArrayList();

/// <summary>
/// Gets or sets the list of property objects for this Properties
object.
/// </summary>
[XmlElement("Property")]
public Property[] Property
{
get
{
Property[] propertiesObj = new Property[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;

Property[] propertiesObj = (Property[])value;
properties.Clear();
foreach (Property property in propertiesObj)
properties.Add(property);
}
}
}

/// <summary>
/// Describes a property object.
/// </summary>
public class Property
{
private string _name = string.Empty;
private string _value = string.Empty;

/// <summary>
/// Gets or sets the name of the property.
/// </summary>
[XmlAttribute("Name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

/// <summary>
/// Gets or sets the value of the property.
/// </summary>
[XmlAttribute("Value")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
}

This looks like this when serialied into Xml:
<Message Type="System" SubType="ID01">
<Properties>
<Property Name="PhoneNumber" Value="0013343222>
</Property>
</Properties>
</Message>

What I want in the above case is I know ID01 can be deserialied into ID01
class so this class looks for a property name of "PhoneNumber" and sets the
PhoneNumber property to this value.

Does anyone know how to achieve this?

Regards
Simon.

Nov 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I've gone through the whole process recently... pretty much the only
way to do it the way you have in mind is to use the copy constructor
and do it manually - that is, the basic object provides methods to
convert the object into a number of other object types, and you just do
conditional code based on whichever of those is valid.... or, for a
more clean, internally polymorphic implementation, you move the
conditional logic inside the instances.

Alternately, you can let the XmlSerializer do the polymorphism for you,
but then the methodology is a little different. Define your specific
implementations as subclasses of the general (possibly abstract) base
class. Then use the XmlIncludeAttribute on the base class to define
the allowed subclasses. These subclasses are then referenced using the
xsi:type="" element... so the type isn't inferred, as in your case, but
explicitly mentioned.

Simon Hart wrote:
Hi,

I am trying to implement some functionality as seen in MS CRM 3.0 whereby a
basic Xml is deserialized into an object which contains properties. What I
want to do from here is; cast the basic object into a more strongly typed
class which knows what properties to expect.

The basic top level class is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections;

namespace MessageOutgoing
{
[XmlRoot(Namespace="")]
/// <summary>
/// Describes the Message object.
/// </summary>
public class Message
{
private MessageType _type;
private MessageSubType _subType;
private ArrayList properties = new ArrayList();

/// <summary>
/// Gets or sets the MessageType enumeration.
/// </summary>
[XmlAttribute("Type")]
public MessageType MessageType
{
get
{
return _type;
}
set
{
_type = value;
}
}

/// <summary>
/// Gets or sets the type of message ie: ID01 - Reset request.
/// </summary>
[XmlAttribute("SubType")]
public MessageSubType MessageSubType
{
get
{
return _subType;
}
set
{
_subType = value;
}
}

/// <summary>
/// Gets or sets the properties collection. A properties collection
/// is a collection of property objects that make up a record.
/// </summary>
[XmlElement("Properties")]
public Properties[] Properties
{
get
{
Properties[] propertiesObj = new Properties[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;

Properties[] propertiesObj = (Properties[])value;
properties.Clear();
foreach (Properties property in propertiesObj)
properties.Add(property);
}
}
}

/// <summary>
/// Describes a collection of property objects.
/// </summary>
public class Properties
{
private ArrayList properties = new ArrayList();

/// <summary>
/// Gets or sets the list of property objects for this Properties
object.
/// </summary>
[XmlElement("Property")]
public Property[] Property
{
get
{
Property[] propertiesObj = new Property[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;

Property[] propertiesObj = (Property[])value;
properties.Clear();
foreach (Property property in propertiesObj)
properties.Add(property);
}
}
}

/// <summary>
/// Describes a property object.
/// </summary>
public class Property
{
private string _name = string.Empty;
private string _value = string.Empty;

/// <summary>
/// Gets or sets the name of the property.
/// </summary>
[XmlAttribute("Name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

/// <summary>
/// Gets or sets the value of the property.
/// </summary>
[XmlAttribute("Value")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
}

This looks like this when serialied into Xml:
<Message Type="System" SubType="ID01">
<Properties>
<Property Name="PhoneNumber" Value="0013343222>
</Property>
</Properties>
</Message>

What I want in the above case is I know ID01 can be deserialied into ID01
class so this class looks for a property name of "PhoneNumber" and sets the
PhoneNumber property to this value.

Does anyone know how to achieve this?

Regards
Simon.
Nov 7 '06 #2

P: n/a
Martin,

Thanks for the reply, do you mean implement some sort of CopyTo() method?

This must be possible as Microsoft has done it in CRM 3.0 SDK.

Regards
Simon.

"Martin Z" wrote:
I've gone through the whole process recently... pretty much the only
way to do it the way you have in mind is to use the copy constructor
and do it manually - that is, the basic object provides methods to
convert the object into a number of other object types, and you just do
conditional code based on whichever of those is valid.... or, for a
more clean, internally polymorphic implementation, you move the
conditional logic inside the instances.

Alternately, you can let the XmlSerializer do the polymorphism for you,
but then the methodology is a little different. Define your specific
implementations as subclasses of the general (possibly abstract) base
class. Then use the XmlIncludeAttribute on the base class to define
the allowed subclasses. These subclasses are then referenced using the
xsi:type="" element... so the type isn't inferred, as in your case, but
explicitly mentioned.

Simon Hart wrote:
Hi,

I am trying to implement some functionality as seen in MS CRM 3.0 whereby a
basic Xml is deserialized into an object which contains properties. What I
want to do from here is; cast the basic object into a more strongly typed
class which knows what properties to expect.

The basic top level class is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections;

namespace MessageOutgoing
{
[XmlRoot(Namespace="")]
/// <summary>
/// Describes the Message object.
/// </summary>
public class Message
{
private MessageType _type;
private MessageSubType _subType;
private ArrayList properties = new ArrayList();

/// <summary>
/// Gets or sets the MessageType enumeration.
/// </summary>
[XmlAttribute("Type")]
public MessageType MessageType
{
get
{
return _type;
}
set
{
_type = value;
}
}

/// <summary>
/// Gets or sets the type of message ie: ID01 - Reset request.
/// </summary>
[XmlAttribute("SubType")]
public MessageSubType MessageSubType
{
get
{
return _subType;
}
set
{
_subType = value;
}
}

/// <summary>
/// Gets or sets the properties collection. A properties collection
/// is a collection of property objects that make up a record.
/// </summary>
[XmlElement("Properties")]
public Properties[] Properties
{
get
{
Properties[] propertiesObj = new Properties[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;

Properties[] propertiesObj = (Properties[])value;
properties.Clear();
foreach (Properties property in propertiesObj)
properties.Add(property);
}
}
}

/// <summary>
/// Describes a collection of property objects.
/// </summary>
public class Properties
{
private ArrayList properties = new ArrayList();

/// <summary>
/// Gets or sets the list of property objects for this Properties
object.
/// </summary>
[XmlElement("Property")]
public Property[] Property
{
get
{
Property[] propertiesObj = new Property[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;

Property[] propertiesObj = (Property[])value;
properties.Clear();
foreach (Property property in propertiesObj)
properties.Add(property);
}
}
}

/// <summary>
/// Describes a property object.
/// </summary>
public class Property
{
private string _name = string.Empty;
private string _value = string.Empty;

/// <summary>
/// Gets or sets the name of the property.
/// </summary>
[XmlAttribute("Name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

/// <summary>
/// Gets or sets the value of the property.
/// </summary>
[XmlAttribute("Value")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
}

This looks like this when serialied into Xml:
<Message Type="System" SubType="ID01">
<Properties>
<Property Name="PhoneNumber" Value="0013343222>
</Property>
</Properties>
</Message>

What I want in the above case is I know ID01 can be deserialied into ID01
class so this class looks for a property name of "PhoneNumber" and sets the
PhoneNumber property to this value.

Does anyone know how to achieve this?

Regards
Simon.

Nov 7 '06 #3

P: n/a
I haven't touched CRM 3.0, but I imagine that it must be done manually
(or from some hefty code-generation). Personally, I find the
polymorphic solution using the native functionality of the
XmlSerializer to be better.

Thought of another, non-polymorphic way to do it that's a little less
boilerplate - skipping how you'd determine the type to construct, since
that's really up to you (one app I worked on just had the full
type/assembly reference included as a member). The approach works like
this: you make all your specific classes wrappers for the basic,
general object. The specific classes have no fields, they read
everything from the base. Then you only have to write one copy
constructor - the base object. You just write properties for all the
members of the specific classes that redirect into the collections of
the base object. Probably it'd be best to include code in the base
class that lazily constructs a dictionary out of the properties array
to make those specific-class-properties less painful.

Simon Hart wrote:
Martin,

Thanks for the reply, do you mean implement some sort of CopyTo() method?

This must be possible as Microsoft has done it in CRM 3.0 SDK.

Regards
Simon.

"Martin Z" wrote:
I've gone through the whole process recently... pretty much the only
way to do it the way you have in mind is to use the copy constructor
and do it manually - that is, the basic object provides methods to
convert the object into a number of other object types, and you just do
conditional code based on whichever of those is valid.... or, for a
more clean, internally polymorphic implementation, you move the
conditional logic inside the instances.

Alternately, you can let the XmlSerializer do the polymorphism for you,
but then the methodology is a little different. Define your specific
implementations as subclasses of the general (possibly abstract) base
class. Then use the XmlIncludeAttribute on the base class to define
the allowed subclasses. These subclasses are then referenced using the
xsi:type="" element... so the type isn't inferred, as in your case, but
explicitly mentioned.

Simon Hart wrote:
Hi,
>
I am trying to implement some functionality as seen in MS CRM 3.0 whereby a
basic Xml is deserialized into an object which contains properties. What I
want to do from here is; cast the basic object into a more strongly typed
class which knows what properties to expect.
>
The basic top level class is as follows:
>
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections;
>
namespace MessageOutgoing
{
[XmlRoot(Namespace="")]
/// <summary>
/// Describes the Message object.
/// </summary>
public class Message
{
private MessageType _type;
private MessageSubType _subType;
private ArrayList properties = new ArrayList();
>
/// <summary>
/// Gets or sets the MessageType enumeration.
/// </summary>
[XmlAttribute("Type")]
public MessageType MessageType
{
get
{
return _type;
}
set
{
_type = value;
}
}
>
/// <summary>
/// Gets or sets the type of message ie: ID01 - Reset request.
/// </summary>
[XmlAttribute("SubType")]
public MessageSubType MessageSubType
{
get
{
return _subType;
}
set
{
_subType = value;
}
}
>
/// <summary>
/// Gets or sets the properties collection. A properties collection
/// is a collection of property objects that make up a record.
/// </summary>
[XmlElement("Properties")]
public Properties[] Properties
{
get
{
Properties[] propertiesObj = new Properties[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;
>
Properties[] propertiesObj = (Properties[])value;
properties.Clear();
foreach (Properties property in propertiesObj)
properties.Add(property);
}
}
}
>
/// <summary>
/// Describes a collection of property objects.
/// </summary>
public class Properties
{
private ArrayList properties = new ArrayList();
>
/// <summary>
/// Gets or sets the list of property objects for this Properties
object.
/// </summary>
[XmlElement("Property")]
public Property[] Property
{
get
{
Property[] propertiesObj = new Property[properties.Count];
properties.CopyTo(propertiesObj);
return propertiesObj;
}
set
{
if (value == null)
return;
>
Property[] propertiesObj = (Property[])value;
properties.Clear();
foreach (Property property in propertiesObj)
properties.Add(property);
}
}
}
>
/// <summary>
/// Describes a property object.
/// </summary>
public class Property
{
private string _name = string.Empty;
private string _value = string.Empty;
>
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
[XmlAttribute("Name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
>
/// <summary>
/// Gets or sets the value of the property.
/// </summary>
[XmlAttribute("Value")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
}
>
This looks like this when serialied into Xml:
>
>
<Message Type="System" SubType="ID01">
<Properties>
<Property Name="PhoneNumber" Value="0013343222>
</Property>
</Properties>
</Message>
>
What I want in the above case is I know ID01 can be deserialied into ID01
class so this class looks for a property name of "PhoneNumber" and sets the
PhoneNumber property to this value.
>
Does anyone know how to achieve this?
>
Regards
Simon.
Nov 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.