更新時間:2021-08-19 11:34:39 來源:動力節點 瀏覽938次
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編程,感興趣的同學可以關注一下。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習