Edit maxOccurs attribute in DataContract

Topics: General Discussion Forum, Service Factory Modeling Edition Forum
Feb 9, 2010 at 6:02 AM

While using WSSF, the default value of maxOccurs when we create a datacontract collection is "unbounded".

<xs:element minOccurs="0" maxOccurs="unbounded" name="Items"  type="tns:Item" />

The class file generated is very abstract with no option for maxOccur edit.

public partial class Items: System.Collections.Generic.List<Item>

How can I edit this value of the maxOccurs attribute (I want to have the maximum number of Item as 4)? 

Feb 10, 2010 at 6:10 PM


There's no an easy way to set that from the generated attribute since that is basically handled by the DataContractSerializer which is lower level than the DC definiton of your generated collection.

You may also try implementing IXmlSerializable from you generated partial class and try to manipulate the xml generation (XmlWrite/XmlRead methods) in your class.

So if you need to modify the generated class template, you can edit the DataContractCollection.tt file located in "..\Data Contract DSL\Dsl\TextTemplates\WCF\CS" and add the serializtion.

You may also get some further ideas from here http://msdn.microsoft.com/en-us/library/ms733112.aspx (bottom sample).