deserialization of xml with unknown object type?

  • Thread starter Thread starter KenK
  • Start date Start date
K

KenK

Hi all,

Been reading all day about object serialization techniques in C# and my
problem is that everything I read is requiring a "known" object class. Is
there a way to create an object from the xml code itself without casting it
through an object?

If not, what is the best way to allow the end user to create their own
object classes?

Thanks,
Ken
 
Ken,

You can use true serialization (using an implementation of IFormatter)
and you can have fields of type object which are serialized/deserialized
correctly. However, with XML serialization, you need to know the types of
the objects being exposed, because of the way that it works (using public
properties on the exposed type).

Hope this helps.
 
KenK said:
Hi all,

Been reading all day about object serialization techniques in C# and my
problem is that everything I read is requiring a "known" object class. Is
there a way to create an object from the xml code itself without casting it
through an object?

If not, what is the best way to allow the end user to create their own
object classes?

Thanks,
Ken

If you want to serialize data using XML then you have to have the requisite
information about the 'types' represented in XML.
Visual Studio can/does this for you. The 'types' I refer to will be likely a
reflection of an underlying datalayer that provides relational information
to the calling class about the tables in that data layer. Table names and
table column names lend themselves well to XML structured documents.

Not sure what you mean about 'allow the end user to create their own object
classes?'

LW
 
thanks,

The idea is that the end user opens an xml file and the content of that xml
file would be displayed in a propertygrid with the xml field names
representing names in the propertygrid and the values would be in the right
(values) column.

Thanks again,
Ken
 
Ken,

In this case, you don't necessarily need to use serialization for this.

You can create a class that implements ICustomTypeDescriptor and exposes
the properties to show in the property grid.

You just have your class load the XML file, and then have the
GetProperties method return the properties that correspond to the data in
the file.

Hope this helps.
 
thank you much. I will investigate this.

ken

Nicholas Paldino said:
Ken,

In this case, you don't necessarily need to use serialization for this.

You can create a class that implements ICustomTypeDescriptor and
exposes the properties to show in the property grid.

You just have your class load the XML file, and then have the
GetProperties method return the properties that correspond to the data in
the file.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

KenK said:
thanks,

The idea is that the end user opens an xml file and the content of that
xml file would be displayed in a propertygrid with the xml field names
representing names in the propertygrid and the values would be in the
right (values) column.

Thanks again,
Ken
 
Back
Top