Blog Home  Home Feed your aggregator (RSS 2.0)  
What did you learn today? - XML Overrides
Phil Denoncourt's Technology Rants
 
 Monday, October 03, 2005
XML Overrides by phildenoncourt
There are a lot of areas in the .NET framework that I haven't paid attention to because I don't have come across a need for a particular area (the WMI stuff) and/or the area looked somewhat complicated. In the XML Serialization engine, there is support for "Overrides". The serialization that I had done to date had worked fine, so I glossed over these objects when I was doing research.

I've written a data layer for my current project. It's a pretty robust data layer if I do say so myself. It has support for parent/child relationships, data transactions, delay loading, cascading deletes, exports to datasets and it's fully generated using a code generator (
MyGeneration).... It's really the Cadillac of data layers.

When an object is serialized, I prevent the serialization of child objects by adding the [XMLIgnore] attribute to the child collection properties. I do this for performance and size reasons. Because the data layer is
delay loaded, the collection might not be populated yet, causing tremendous database activity when a developer decides to serialize the object. Also, if you serialize an object high in the hierarchy, you would end up getting a hugely sized piece of XML. The children of the children of the children would be included in the document. After patting myself on the back for writing a fully featured, lightweight, fast and consistent data layer quickly with relatively few defects, the need came down for the objects to include all of their children in special circumstances... But only select child objects, not all.

I gave this some thought over the weekend and resigned myself to the fact that I was going to have to write some specialized method that serialized each object individually and then molded all the results into one big XML document. After doing some more reading, I came across the
XmlAttributeOverrides object. This object allows you to selectively override XML serialization attributes that you set for specific properties. Using this object, I created a serializer, told it which additional properties I wanted serialized, and BAM!.. Done.


			//Set up overrides
XmlAttributeOverrides overrides = new XmlAttributeOverrides();

//ChildItems
XmlAttributes dontIgnore = new XmlAttributes();
dontIgnore.XmlIgnore=
false;
overrides.Add(
typeof(ParentObject),"ChildItems",dontIgnore); XmlSerializer xs = new XmlSerializer(typeof(ParentObject),overrides);
Monday, October 03, 2005 8:25:06 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Development | DotNet  | 
Copyright © 2010 Phil Denoncourt III. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: