Serialize and Deserialize XML using dot.net c#

老子叫甜甜 提交于 2019-12-01 15:10:34

I suggest moving the type parameter T to the enclosing class and making the XmlSerializer instance static. A static field in a generic class is per closed type, so SerializationHelper<Apple> and SerializationHelper<Orange> will each have separate instances of the field.

Also, I'm not sure that catch { return String.Empty; } is the best idea either -- masking problems to avoid crashing makes me nervous.

I think there is no need for the whole Encoding part. You simply serialise using one encoding, then convert to bytes, and then convert back to Unicode. Why is that? But I might be missing something here.

Another thing that hits me is .ToArray() usage. If you have big hiearchy and serialising lot of objects, this can be pretty performance heavy. Try using StreamReader to read the memory stream without need to copy it into to Array. But this requires some performance testing to back up my reasoning.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!