1

We're writing a .NET 3.5 application, which uses Xml serialization of various objects.

We are basically creating an xml document from an object, and i'd like to be able to use this attribute (DefaultParameterValue) which is what .NET 4.0 is using to implement optional arguments.

I'd like the Xml generated document to contain the default values for the parameters that have this attribute.

Is it possible?

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • try looking at this (http://stackoverflow.com/questions/2385634/xsd-exe-generated-classes-dont-serialize-default-value-attributes) – DiVan Jun 26 '11 at 16:18

2 Answers2

3

XmlSerializer operates at the (public) field/property level. In doing so, one of the things it looks at is the similar [DefaultValue(...)]. It does not look at methods at all, except for a few assistance patterns like ShouldSerialize*(). As such, there would seen to be no crossover at all with parameters, ad no need to look at [DefaultParameterValue(...)].

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

This is not how attributes work. The ones that xml de/serializers recognize are well documented, the list is not extensible. Adding more requires changing code. Code that you cannot change, it is locked up inside a framework assembly.

Implement the equivalent by assigning the default value you want in the class constructor.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Is the XmlSerializer customizable in any way like some of the .NET built in classes? – lysergic-acid Jun 26 '11 at 21:00
  • It has a few virtual methods. You'll find them quite useless to implement the feature you want. System.Xml is not .NET's greatest achievement, it has been largely replaced. – Hans Passant Jun 26 '11 at 21:14