C# xml与对象相互转换

2021-03-21 02:25

阅读:691

标签:gif   throw   linq   try   serial   处理   splay   odi   read   

例如:

  1.对象转xml(对象序列化为xml)

技术图片技术图片
string strImage=  XmlSerializeHelper.Serialize>(imageList);
View Code

  2.xml转对象(反序列化)

技术图片技术图片
Image bojimag= XmlSerializeHelper.DeSerialize(strimage);
View Code

 

该序列化处理类如下:

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;

namespace Helper
{
    public class XmlSerializeHelper
    {

        public static string Serialize(T obj)
        {
            return Serialize(obj, Encoding.UTF8);
        }

        //对象转XML
        public static string ObjToXml(object obj)
        {
            using (MemoryStream Stream = new MemoryStream())
            {
                XmlSerializer xml = new XmlSerializer(obj.GetType());
                xml.Serialize(Stream, obj);
                Stream.Position = 0;
                StreamReader sr = new StreamReader(Stream);
                string str = sr.ReadToEnd();
                return str;
            }

        }

        ///   
        /// 实体对象序列化成xml字符串  
        ///   
        ///   
        ///   
        ///   
        public static string Serialize(T obj, Encoding encoding)
        {
            try
            {

                if (obj == null)
                    throw new ArgumentNullException("obj");

                var ser = new XmlSerializer(obj.GetType());
                using (var ms = new MemoryStream())
                {
                    using (var writer = new XmlTextWriter(ms, encoding))
                    {
                        writer.Formatting = Formatting.Indented;
                        ser.Serialize(writer, obj);
                    }
                    var xml = encoding.GetString(ms.ToArray());
                    xml = xml.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
                    xml = xml.Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");
                    xml=Regex.Replace(xml,@"\s{2}","");
                    xml = Regex.Replace(xml, @"\s{1}/>", "/>");
                    return xml;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///   
        /// 反序列化xml字符为对象,默认为Utf-8编码  
        ///   
        ///   
        ///   
        ///   
        public static T DeSerialize(string xml)
            where T : new()
        {
            return DeSerialize(xml, Encoding.UTF8);
        }

        ///   
        /// 反序列化xml字符为对象  
        ///   
        ///   
        ///   
        ///   
        ///   
        public static T DeSerialize(string xml, Encoding encoding)
            where T : new()
        {
            try
            {
                var mySerializer = new XmlSerializer(typeof(T));
                using (var ms = new MemoryStream(encoding.GetBytes(xml)))
                {
                    using (var sr = new StreamReader(ms, encoding))
                    {
                        return (T)mySerializer.Deserialize(sr);
                    }
                }
            }
            catch (Exception e)
            {
                return default(T);
            }

        }  
    }
}
XmlSerializeHelper.cs

 

C# xml与对象相互转换

标签:gif   throw   linq   try   serial   处理   splay   odi   read   

原文地址:https://www.cnblogs.com/muyeh/p/11832298.html


评论


亲,登录后才可以留言!