////// 实体类序列化的反序列化的类 /// /// public abstract class EntityClassXmlSerializer { ////// 实体类序列化成xml string /// /// /// public static string ToXMLString(T entity) { using (MemoryStream stream = new MemoryStream()) { XmlTextWriter writer = new XmlTextWriter(stream, null); XmlSerializer xml = new XmlSerializer(entity.GetType()); xml.Serialize(writer, entity); writer.Formatting = Formatting.Indented; using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8)) { stream.Position = 0; string xmlString = sr.ReadToEnd(); sr.Close(); stream.Close(); return xmlString; } } } ////// 实体类反序列化 /// /// /// public static T ReadFromXML(string xml) { T entity; byte[] byts = Encoding.UTF8.GetBytes(xml); using (MemoryStream stream = new MemoryStream(byts)) { XmlSerializer xs = new XmlSerializer(typeof(T)); entity = (T)xs.Deserialize(stream); return entity; } } }
以上就是C# 实体类序列化与反序列化一 (XmlSerializer)的内容,更多相关内容请关注PHP中文网(www.php.cn)!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432298.html
微信扫一扫
支付宝扫一扫