這篇文章給大家介紹如何在C#項(xiàng)目中實(shí)現(xiàn)對(duì)象序列化XML,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都創(chuàng)新互聯(lián)2013年至今,先為沈丘等服務(wù)建站,沈丘等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為沈丘企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。首先,需要用到的是這兩個(gè)命名空間(主要)
using System.Xml; using System.Xml.Serialization;
然后序列化和反序列化的方式和Json一樣。(后面提供封裝方法)
string result = XmlSerializeHelper.Serialize<test>(new test { name = "Seven", cardId = "663927", age = 15, currentTime = DateTime.Now }); Console.WriteLine(result); test test2 = XmlSerializeHelper.DeSerialize<test>(result);
下面來(lái)看封裝類
public class XmlSerializeHelper { //對(duì)象轉(zhuǎn)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); return sr.ReadToEnd(); } } public static string Serialize<T>(T obj) { return Serialize<T>(obj, Encoding.UTF8); } /// <summary> /// 實(shí)體對(duì)象序列化成xml字符串 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string Serialize<T>(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; } } /// <summary> /// 反序列化xml字符為對(duì)象,默認(rèn)為Utf-8編碼 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xml"></param> /// <returns></returns> public static T DeSerialize<T>(string xml) where T : new() { return DeSerialize<T>(xml, Encoding.UTF8); } /// <summary> /// 反序列化xml字符為對(duì)象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xml"></param> /// <param name="encoding"></param> /// <returns></returns> public static T DeSerialize<T>(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); } } } Class Of XmlSerializeHelper
關(guān)于如何在C#項(xiàng)目中實(shí)現(xiàn)對(duì)象序列化XML就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞名稱:如何在C#項(xiàng)目中實(shí)現(xiàn)對(duì)象序列化XML-創(chuàng)新互聯(lián)
本文來(lái)源:http://jinyejixie.com/article48/hgohp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、小程序開(kāi)發(fā)、網(wǎng)站改版、網(wǎng)站策劃、全網(wǎng)營(yíng)銷推廣、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容