Automatic truncation of attribute / element names when using XmlSerializer
When you serialize a C # class using the XmlSerializer, the attributes / elements that represent the properties of the class will have the same names as in the source code.
I know that you can override this by doing this:
[XmlAttribute("num")]
public int NumberOfThingsThatAbcXyz { get; set; }
I would like the generated XML for my classes to be as compact as possible, but obviously still capable of being automatically deserialized on the other side.
Is there a way for these names to be minimized as much as possible, without having to manually think and comment everything with a short string? The resulting XML, which is human readable, is not a concern.
a source to share
Hmya, watch out for micro-optimizations like this. It is incredibly difficult to get them to pay off with modern computing hardware. The true cost of I / O associated with XML processing is not the amount of data, but the localization. Bytes are cheap, their transfer is already optimized. It's the head of a disk reader, shrinking on a server somewhere, perhaps on your own machine, that determines how fast your program runs. Once it's in the right place, reading a byte is almost as expensive as reading a kilobyte. Memory works the same way.
Final hint: if this was a general optimization, they would have come up with something better than IXmlSerializable.
a source to share
To customize XML serialization beyond what you can do using attributes, you will need your classes to implement IXmlSerializable .
a source to share
If human readability is not an issue ... why bother using xml at all - perhaps something like "protocol buffers" (there are C # implementations available) will give you the level of compatibility you want without this problem, including some ways to automatically get all properties (i I can tell you more about this if you are interested).
Another alternative is to simply gzip the data; xml zips is surprisingly good, thanks to all the duplicate node names.
a source to share
The nature of the attributes implies that this is not possible. See can-attributes-be-added-dynamic-in-C # for what. Even if it could be done, I agree with Hans that it probably isn't worth it. If you are really concerned about this, you should go with one of Mark's suggestions.
a source to share