Is it possible to make [XmlAttribute] as default for a class?
I have a lot of properties to serialize using XmlSerializer
. I need to tag each (simple) property with [XmlAttribute]
to make the output a little shorter.开发者_如何转开发 I'm wondering if there is an easier way to do it. Is there a way to make it as a default for a class, so that all simple type properties will be serialized as attribute instead of element?
No, basically. Well, you could do the work at runtime using XmlAttributeOverrides
, inspecting your types in code and setting the attributes appropriately, then making sure you cache the serializer (if you don't cache when using XmlAttributeOverrides
you leak memory, as the generated assemblies aren't collected). But that sounds like more work.
Re making the output shorter; xml compresses with GZip or Deflate very nicely...
Of course, if you don't need xml, but just need the data serialized somehow, there are other options.
You can create code snippets.
Create one for properties with the attribute already set, and use it when writing them.
精彩评论