Thursday, June 24, 2004

Following the idea of displaying a class in a PropertyGrid, we can also take a look at how it serializes using the XmlSerializer (see if it serializes at all!). The task looks trivial at first sight:

  1. create an instance of the object,
  2. create a XmlSerializer that can serialize it,
  3. write the object to a XmlRichTextWriter to get colors :)

Of course, there's a gotcha here. You will likely get empty xml files all of the time because usually, the default constructor creates "empty" object. So what we want is to dress the object before serializing them. This is done in a variety of ways depending on the property type:

  • the property/field is a string, int, etc... you can set easily a value to it,
  • the property/field is tagged with XmlArrayAttribute,
    • the property/field is an array: get all the XmlArrayItem attribute, create an array and fill it with instance of those items,
    • the property/field is a collection: get the "Add(Object)" method, add instances of the items.

Of course, this is rather simplistic and may fail on forgotten situations.

Screenshot: ReportAssembly contains collection properties (namespaces, fixtures),  which would appear empty if not "dressed".

posted on Thursday, June 24, 2004 9:23:00 PM UTC  #    Comments [2]
Tracked by:
"genital wart removal" (online) [Trackback]
Monday, June 06, 2005 5:53:50 PM UTC
James Geurts' Blog
Monday, June 06, 2005 5:53:50 PM UTC
Peli's Blog
Comments are closed.