.Net 基础 XML 序列化 反序列化

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;





using System.Diagnostics;

using Ebao51Net.Po.OrderModule;

using Ebao51Net.Po.FinanceModule;

using System.IO;

using System.Xml.Serialization;





using System.Xml;



namespace CoreProxy

{

    public class ServiceProxy : ProxyBase

    {



        static string token;









        public int PlaceOrder(string TargetOut, string ArrayOfOrderDetail, string Order, string OrderClient)

        {





            XmlDocument xdArrayOfOrderDetail = new XmlDocument();

            xdArrayOfOrderDetail.LoadXml(ArrayOfOrderDetail);



            XmlSerializer xsOrder = new XmlSerializer(typeof(Order));

            Order order = (Order)xsOrder.Deserialize(new MemoryStream(ASCIIEncoding.Default.GetBytes(Order)));



            //反序列化

            XmlSerializer xsClient = new XmlSerializer(typeof(OrderClient));

            OrderClient client = (OrderClient)xsClient.Deserialize(new MemoryStream(ASCIIEncoding.Default.GetBytes(OrderClient)));



            token = base.AuthManager.Login("admin", "1234567");

            //int id = OrderManager.AddOrderMore(order, client, client, client, detaillist, token);





            //序列化

            //string l = Serialize<List<OrderDetail>>(list);

            //string l1 = Serialize<OrderDetail>(detail2);

            //string l2 = Serialize<OrderClient>(client);



            return 1;

        }



        public string Serialize<T>(T t)

        {

            using (StringWriter sw = new StringWriter())

            {

                XmlSerializer xz = new XmlSerializer(t.GetType());

                xz.Serialize(sw, t);

                string xs = ChangeXmlStr(sw.ToString());

                return xs;

            }

        }



        private string ChangeXmlStr(string xmlstr)

        {

            ////替换编码标示符

            //string strchange1 = xmlstr.Replace("encoding=\"utf-16\"", "encoding=\"utf-8\"");

            //去掉网页地址

            string strchange2 = xmlstr.Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");

            //替换root

            //string strchange3 = strchange2.Replace("ArrayOfUser", "dataset");

            return strchange2;

        }

    }

}

你可能感兴趣的:(.net)