博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML Serialize/Deserialize
阅读量:5059 次
发布时间:2019-06-12

本文共 2095 字,大约阅读时间需要 6 分钟。

 

 

using System;using System.Collections.Generic;using System.Globalization;using System.IO;using System.Linq;using System.Text;using System.Web;using System.Xml;using System.Xml.Serialization;namespace Common{    public class XmlConverter    {        public static string Serialize
(T obj) { //XmlSerializer serializer = new XmlSerializer(typeof(T)); //StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); //serializer.Serialize(writer, obj); //string xml = writer.ToString(); //writer.Close(); //writer.Dispose(); //return xml; string OutputXmlString = string.Empty; using (MemoryStream ms = new MemoryStream()) { var setting = new XmlWriterSettings() { Encoding = new UTF8Encoding(false), Indent = true, }; using (XmlWriter writer = XmlWriter.Create(ms, setting)) { XmlSerializer xmlSearializer = new XmlSerializer(typeof(T)); xmlSearializer.Serialize(writer, obj); OutputXmlString = Encoding.UTF8.GetString(ms.ToArray()); } } return OutputXmlString; } public static T Deserialize
(string xml) { //XmlSerializer serializer = new XmlSerializer(typeof(T)); //StringReader reader = new StringReader(xml); //T result = (T)(serializer.Deserialize(reader)); //reader.Close(); //reader.Dispose(); //return result; using (MemoryStream MS = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { using (XmlReader xr = XmlReader.Create(MS)) { XmlSerializer xmlSearializer = new XmlSerializer(typeof(T)); return (T)xmlSearializer.Deserialize(xr); } } } }}

 

转载于:https://www.cnblogs.com/cicada/p/10868272.html

你可能感兴趣的文章
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>
WPF星空效果
查看>>
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>
几款Http小服务器
查看>>
iOS 数组排序
查看>>
第三节
查看>>
PHP结合MYSQL记录结果分页呈现(比较实用)
查看>>
Mysql支持的数据类型
查看>>
openSuse beginner
查看>>
Codeforces 620E(线段树+dfs序+状态压缩)
查看>>
Windows7中双击py文件运行程序
查看>>
Market entry case
查看>>
bzoj1230 开关灯 线段树
查看>>
LinearLayout
查看>>
学习python:day1
查看>>
css3动画属性
查看>>
第九次团队作业-测试报告与用户使用手册
查看>>