Improving XML serialization function