大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 XML轉換為對象操作類詳解

XML轉換為對象操作類詳解

更新時間:2021-08-19 11:34:39 來源:動力節點 瀏覽991次

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);
? ? ? ? }
? ? }
}

以上就是動力節點小編介紹的"XML轉換為對象操作類詳解",希望對大家有幫助,想了解更多可查看Java在線學習。動力節點在線學習教程,針對沒有任何Java基礎的讀者學習,讓你從入門到精通,主要介紹了一些Java基礎的核心知識,讓同學們更好更方便的學習和了解Java編程,感興趣的同學可以關注一下。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产精品欧美亚洲韩国日本 | 草久在线观看视频 | 日日操夜夜操免费视频 | 亚洲午夜天堂 | 欧美成人性videos | 久久久久爽亚洲精品 | 午夜aaaaaaaaa视频在线 | 欧美午夜在线观看理论片 | 欧美一级别 | 日本级毛片免费观看 | 久久我们这里只有精品国产4 | 亚洲国产精品综合久久2007 | 最新69成人精品毛片 | 久久国产精品一国产精品金尊 | 国产 欧美 日产中文 | 欧美xxxxx性另类 | 国产成人女人视频在线观看 | 欧美一级艳片视频免费观看 | 免费国产小视频在线观看 | 99热这就是里面只有精品 | 奇米第四色视频 | 一品毛片 | 韩国精品一区二区久久 | 日韩欧美手机在线 | 波多野结衣中文字幕一区二区 | 中文字幕一区二区三区永久 | 亚洲在线免费观看 | 精品网站 | 三级aaa黄特色 | www国产 | 中文字幕网在线 | 一级片一级毛片 | 久久99精品久久久久久牛牛影视 | 97色精品视频在线观看免费 | 国产网站视频 | 久久伊人中文字幕有码 | 日韩综合在线 | 国产精品福利在线观看免费不卡 | 美女一级毛片免费不卡视频 | 日本欧美在线观看 | 国产精品亚洲欧美大片在线看 |