专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 XML转换为对象操作类详解

XML转换为对象操作类详解

更新时间:2021-08-19 11:34:39 来源:赢咖4 浏览726次

XML转换为对象操作类

XML与Object转换类 

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace WebApplication1
{
    public sealed class XMLSerilizable
    {
        /// <summary>
        /// 将object对象序列化成XML
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string ObjectToXML<T>(T t, Encoding encoding)
        {
            XmlSerializer ser = new XmlSerializer(t.GetType());
            using (MemoryStream mem = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(mem, encoding))
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    ser.Serialize(writer, t, ns);
                    return encoding.GetString(mem.ToArray()).Trim();
                }
            }
        }
        /// <summary>
        /// 将XML反序列化成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static T XMLToObject<T>(string source, Encoding encoding)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            using (MemoryStream stream = new MemoryStream(encoding.GetBytes(source)))
            {
                return (T)mySerializer.Deserialize(stream);
            }
        }
        /// <summary>
        /// 二进制方式序列化对象
        /// </summary>
        /// <param name="testUser"></param>
        public static string Serialize<T>(T obj)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            return ms.ToString();
        }
        /// <summary>
        /// 二进制方式反序列化对象
        /// </summary>
        /// <returns></returns>
        public static T DeSerialize<T>(string str) where T : class
        {
            MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str));
            BinaryFormatter formatter = new BinaryFormatter();
            T t = formatter.Deserialize(ms) as T;
            return t;
        }

    }
} 

测试类

using System;
using System.Xml.Serialization;
namespace WebApplication1
{
    [Serializable]
    [XmlRoot("Result")]
    public class XMLClass
    {
        private string rtnValue;
        [XmlElement("Value")]
        public string RtnValue
        {
            get { return rtnValue; }
            set { rtnValue = value; }
        }
        private string rtnKey;
        [XmlElement("Key")]
        public string RtnKey
        {
            get { return rtnKey; }
            set { rtnKey = value; }
        }
    }
}

调用

using System;
using System.Text;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            XMLClass xc = new XMLClass();
            xc.RtnKey = "TestXML";
            xc.RtnValue = "ObjectToXML";
            string xml = XMLSerilizable.ObjectToXML<XMLClass>(xc,System.Text.Encoding.UTF8);
            StringBuilder strXML = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            strXML.Append("<Result>");
            strXML.AppendFormat("<Key>{0}</Key>","TestXML");
            strXML.AppendFormat("<Value>{0}</Value>","XMLToObject");
            strXML.Append("</Result>");
            XMLClass xClass = XMLSerilizable.XMLToObject<XMLClass>(strXML.ToString(),System.Text.Encoding.UTF8);
        }
    }
}

以上就是赢咖4小编介绍的"XML转换为对象操作类详解",希望对大家有帮助,想了解更多可查看Java赢咖4在线学习。赢咖4在线学习教程,针对没有任何Java基础的读者学习,让你从入门到精通,主要介绍了一些Java基础的核心知识,让同学们更好更方便的学习和了解Java编程,感兴趣的同学可以关注一下。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>