gomasch Development Notes

Donnerstag, Mai 24, 2007

XML Serializer & Default Attribute Value

Old stuff: The .NET XMLSerializer is quite powerful, but not perfectly documented. Hmm.

I found for example the support for default attribute values *is* there and also more or less complete!

What we want: A class with an instance variable serialized to an attribute:

Untested Example:

[Serializable] [XmlRoot(ElementName="FILE")] public class Example { [XmlAttribute("filename")] [System.ComponentModel.DefaultValueAttribute(FileNameDefault)] public string FileName; public const string FileNameDefault = "default.xml"; public Example() { FileName = FileNameDefault; } }

Remember: to control what's happening you can also always take a look at the code generated by the XMLSerializer.

martin schmidt 16:38 | 0 comments |