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

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) hot資訊 XML轉(zhuǎn)換為對(duì)象操作類詳解

XML轉(zhuǎn)換為對(duì)象操作類詳解

更新時(shí)間:2021-08-19 11:34:39 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1014次

XML轉(zhuǎn)換為對(duì)象操作類

XML與Object轉(zhuǎn)換類 

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對(duì)象序列化成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反序列化成對(duì)象
        /// </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>
        /// 二進(jìn)制方式序列化對(duì)象
        /// </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>
        /// 二進(jìn)制方式反序列化對(duì)象
        /// </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;
        }

    }
} 

測(cè)試類

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

調(diào)用

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

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

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产精品一区二区三区四区五区 | 国产免费一区二区三区免费视频 | 天天做天天爱天天爽综合网 | 日本黄页网站在线观看 | 夜夜夜夜操 | 中文字幕精品在线视频 | 日韩中文字幕精品 | 中国在线播放精品区 | 久久综合免费视频 | 天天狠操 | 久久久久亚洲 | 欧美在线一级片 | 99热久久国产这里是精品 | 久久国产国内精品对话对白 | 涩涩的视频在线观看 | 久久99精品久久久久久水蜜桃 | 国产精品成人观看视频网站 | 伊人黄色 | 国产特黄一级毛片特黄 | 99香蕉国产精品偷在线观看 | 久久性生活片 | 99在线精品免费视频九九视 | 免费看搡女人的视频 | 亚洲欧美久久精品 | 欧美精品大片 | 伊人色在线 | 国产原创麻豆精品视频 | 欧美三级久久 | 四虎影院在线看 | 久久久久综合精品福利啪啪 | 中文字幕在线精品视频入口一区 | 天海翼精品久久中文字幕 | 波多野结衣一区2区3区 | 狠狠色婷婷综合天天久久丁香 | 4huh34四虎最新久 | 色婷婷综合久久久久中文一区二区 | 欧美一级毛片在线观看 | 免费a级毛片大学生免费观看 | 不卡午夜 | 国产高清a毛片在线看 | 青草久久精品 |