序列化和反序列化XML
From:
http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx?ArticleID=f7ce0a0f-49ef-46be-b311-2148715c404c
Serialize.cs 实现序列化和反序列化的具体操作
 using System;
using System;
 using System.IO;
using System.IO;
 using System.Runtime.Serialization;
using System.Runtime.Serialization;
 using System.Xml.Serialization;
using System.Xml.Serialization;
 using System.Xml;
using System.Xml;
 using System.Text;
using System.Text;
 using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Binary;
 //System.Runtime.Serialization
//System.Runtime.Serialization

 namespace WebApplication2
namespace WebApplication2
 {
{

 public class SerializeObject
    public class SerializeObject
 {
    {
 
    
 
        
 /// <summary>
        /// <summary>
 /// Returns the set of included namespaces for the serializer.
        /// Returns the set of included namespaces for the serializer.
 /// </summary>
        /// </summary>
 /// <returns>
        /// <returns>
 /// The set of included namespaces for the serializer.
        /// The set of included namespaces for the serializer.
 /// </returns>
        /// </returns>
 public static XmlSerializerNamespaces GetNamespaces()
        public static XmlSerializerNamespaces GetNamespaces()
 {
        {
 XmlSerializerNamespaces ns;
            XmlSerializerNamespaces ns;
 ns = new XmlSerializerNamespaces();
            ns = new XmlSerializerNamespaces();
 ns.Add("xs", "http://www.w3.org/2001/XMLSchema");
            ns.Add("xs", "http://www.w3.org/2001/XMLSchema");
 ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
 return ns;
            return ns;
 }
        }

 
        
 public static string TargetNamespace
        public static string TargetNamespace
 {
        {
 get
            get
 {
            {
 return "http://www.w3.org/2001/XMLSchema";
                return "http://www.w3.org/2001/XMLSchema";
 }
            }
 }
        }

 
        

 public static string ToXml(object Obj, System.Type ObjType)
        public static string ToXml(object Obj, System.Type ObjType)
 {
        {
 XmlSerializer ser;
            XmlSerializer ser;
 ser = new XmlSerializer(ObjType, SerializeObject.TargetNamespace);
            ser = new XmlSerializer(ObjType, SerializeObject.TargetNamespace);
 MemoryStream memStream;
            MemoryStream memStream;
 memStream = new MemoryStream();
            memStream = new MemoryStream();
 XmlTextWriter xmlWriter;
            XmlTextWriter xmlWriter;
 xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
 xmlWriter.Namespaces = true;
            xmlWriter.Namespaces = true;
 ser.Serialize(xmlWriter, Obj, SerializeObject.GetNamespaces());
            ser.Serialize(xmlWriter, Obj, SerializeObject.GetNamespaces());
 xmlWriter.Close();
            xmlWriter.Close();
 memStream.Close();
            memStream.Close();
 string xml;
            string xml;
 xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
 xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
 xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
 return xml;
            return xml;
 }
        }

 
        
 public static object FromXml(string Xml, System.Type ObjType)
        public static object FromXml(string Xml, System.Type ObjType)
 {
        {
 XmlSerializer ser;
            XmlSerializer ser;
 ser = new XmlSerializer(ObjType);
            ser = new XmlSerializer(ObjType);
 StringReader stringReader;
            StringReader stringReader;
 stringReader = new StringReader(Xml);
            stringReader = new StringReader(Xml);
 XmlTextReader xmlReader;
            XmlTextReader xmlReader;
 xmlReader = new XmlTextReader(stringReader);
            xmlReader = new XmlTextReader(stringReader);
 object obj;
            object obj;
 obj = ser.Deserialize(xmlReader);
            obj = ser.Deserialize(xmlReader);
 xmlReader.Close();
            xmlReader.Close();
 stringReader.Close();
            stringReader.Close();
 return obj;
            return obj;
 }
        }

 }
    }
 }
}
context.cs类结构如下:
 using System;
using System;

 namespace WebApplication2
namespace WebApplication2
 {
{
 
    
 public class context
    public class context
 {
    {
 public string context1;
        public string context1;
 public string context2;
        public string context2;
 public string context3;
        public string context3;
 public string context4;
        public string context4;
 public string context5;
        public string context5;
 public string context6;
        public string context6;


 public context Copy()
        public context Copy()
 {
        {
 context retCtx = new context();
            context retCtx = new context();
 
            
 retCtx.context1 = context1;
            retCtx.context1 = context1;
 retCtx.context2 = context2;
            retCtx.context2 = context2;
 retCtx.context3 = context3;
            retCtx.context3 = context3;
 retCtx.context4 = context4;
            retCtx.context4 = context4;
 retCtx.context5 = context5;
            retCtx.context5 = context5;
 return retCtx;
            return retCtx;
 }
        }
 }
    }
 }
}

 
前台UI调用如下:
序列化:
 string contextxml = @"<context>
    string contextxml = @"<context>
 <context1 xmlns=''>1</context1>
                <context1 xmlns=''>1</context1>
 <context2 xmlns=''>2</context2>
                <context2 xmlns=''>2</context2>
 <context3 xmlns=''>3</context3>
                <context3 xmlns=''>3</context3>
 <context4 xmlns=''>4</context4>
                <context4 xmlns=''>4</context4>
 <context5 xmlns=''>5</context5>
                <context5 xmlns=''>5</context5>
 </context>";
                </context>";
 
            
 con = (context)SerializeObject.FromXml(contextxml,typeof(context));
            con = (context)SerializeObject.FromXml(contextxml,typeof(context));

 Response.Write("Context1 = " + con.context1);
            Response.Write("Context1 = " + con.context1);
 Response.Write("Context2 = " + con.context2);
            Response.Write("Context2 = " + con.context2);
 Response.Write("Context3 = " + con.context3);
            Response.Write("Context3 = " + con.context3);
 Response.Write("Context4 = " + con.context4);
            Response.Write("Context4 = " + con.context4);
 Response.Write("Context5 = " + con.context5);
            Response.Write("Context5 = " + con.context5);
反序列化:
 string str_output=(string)SerializeObject.ToXml(con,typeof(context));
  string str_output=(string)SerializeObject.ToXml(con,typeof(context));
 Response.Write(str_output);
            Response.Write(str_output);
http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx?ArticleID=f7ce0a0f-49ef-46be-b311-2148715c404c
Serialize.cs 实现序列化和反序列化的具体操作
 using System;
using System; using System.IO;
using System.IO; using System.Runtime.Serialization;
using System.Runtime.Serialization; using System.Xml.Serialization;
using System.Xml.Serialization; using System.Xml;
using System.Xml; using System.Text;
using System.Text; using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Binary; //System.Runtime.Serialization
//System.Runtime.Serialization
 namespace WebApplication2
namespace WebApplication2 {
{
 public class SerializeObject
    public class SerializeObject {
    { 
     
         /// <summary>
        /// <summary> /// Returns the set of included namespaces for the serializer.
        /// Returns the set of included namespaces for the serializer. /// </summary>
        /// </summary> /// <returns>
        /// <returns> /// The set of included namespaces for the serializer.
        /// The set of included namespaces for the serializer. /// </returns>
        /// </returns> public static XmlSerializerNamespaces GetNamespaces()
        public static XmlSerializerNamespaces GetNamespaces() {
        { XmlSerializerNamespaces ns;
            XmlSerializerNamespaces ns; ns = new XmlSerializerNamespaces();
            ns = new XmlSerializerNamespaces(); ns.Add("xs", "http://www.w3.org/2001/XMLSchema");
            ns.Add("xs", "http://www.w3.org/2001/XMLSchema"); ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); return ns;
            return ns; }
        }
 
         public static string TargetNamespace
        public static string TargetNamespace {
        { get
            get {
            { return "http://www.w3.org/2001/XMLSchema";
                return "http://www.w3.org/2001/XMLSchema"; }
            } }
        }
 
        
 public static string ToXml(object Obj, System.Type ObjType)
        public static string ToXml(object Obj, System.Type ObjType) {
        { XmlSerializer ser;
            XmlSerializer ser; ser = new XmlSerializer(ObjType, SerializeObject.TargetNamespace);
            ser = new XmlSerializer(ObjType, SerializeObject.TargetNamespace); MemoryStream memStream;
            MemoryStream memStream; memStream = new MemoryStream();
            memStream = new MemoryStream(); XmlTextWriter xmlWriter;
            XmlTextWriter xmlWriter; xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8); xmlWriter.Namespaces = true;
            xmlWriter.Namespaces = true; ser.Serialize(xmlWriter, Obj, SerializeObject.GetNamespaces());
            ser.Serialize(xmlWriter, Obj, SerializeObject.GetNamespaces()); xmlWriter.Close();
            xmlWriter.Close(); memStream.Close();
            memStream.Close(); string xml;
            string xml; xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = Encoding.UTF8.GetString(memStream.GetBuffer()); xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60))); xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1)); return xml;
            return xml; }
        }
 
         public static object FromXml(string Xml, System.Type ObjType)
        public static object FromXml(string Xml, System.Type ObjType) {
        { XmlSerializer ser;
            XmlSerializer ser; ser = new XmlSerializer(ObjType);
            ser = new XmlSerializer(ObjType); StringReader stringReader;
            StringReader stringReader; stringReader = new StringReader(Xml);
            stringReader = new StringReader(Xml); XmlTextReader xmlReader;
            XmlTextReader xmlReader; xmlReader = new XmlTextReader(stringReader);
            xmlReader = new XmlTextReader(stringReader); object obj;
            object obj; obj = ser.Deserialize(xmlReader);
            obj = ser.Deserialize(xmlReader); xmlReader.Close();
            xmlReader.Close(); stringReader.Close();
            stringReader.Close(); return obj;
            return obj; }
        }
 }
    } }
}context.cs类结构如下:
 using System;
using System;
 namespace WebApplication2
namespace WebApplication2 {
{ 
     public class context
    public class context {
    { public string context1;
        public string context1; public string context2;
        public string context2; public string context3;
        public string context3; public string context4;
        public string context4; public string context5;
        public string context5; public string context6;
        public string context6;

 public context Copy()
        public context Copy() {
        { context retCtx = new context();
            context retCtx = new context(); 
             retCtx.context1 = context1;
            retCtx.context1 = context1; retCtx.context2 = context2;
            retCtx.context2 = context2; retCtx.context3 = context3;
            retCtx.context3 = context3; retCtx.context4 = context4;
            retCtx.context4 = context4; retCtx.context5 = context5;
            retCtx.context5 = context5; return retCtx;
            return retCtx; }
        } }
    } }
}

前台UI调用如下:
序列化:
 string contextxml = @"<context>
    string contextxml = @"<context> <context1 xmlns=''>1</context1>
                <context1 xmlns=''>1</context1> <context2 xmlns=''>2</context2>
                <context2 xmlns=''>2</context2> <context3 xmlns=''>3</context3>
                <context3 xmlns=''>3</context3> <context4 xmlns=''>4</context4>
                <context4 xmlns=''>4</context4> <context5 xmlns=''>5</context5>
                <context5 xmlns=''>5</context5> </context>";
                </context>"; 
             con = (context)SerializeObject.FromXml(contextxml,typeof(context));
            con = (context)SerializeObject.FromXml(contextxml,typeof(context));
 Response.Write("Context1 = " + con.context1);
            Response.Write("Context1 = " + con.context1); Response.Write("Context2 = " + con.context2);
            Response.Write("Context2 = " + con.context2); Response.Write("Context3 = " + con.context3);
            Response.Write("Context3 = " + con.context3); Response.Write("Context4 = " + con.context4);
            Response.Write("Context4 = " + con.context4); Response.Write("Context5 = " + con.context5);
            Response.Write("Context5 = " + con.context5);反序列化:
 string str_output=(string)SerializeObject.ToXml(con,typeof(context));
  string str_output=(string)SerializeObject.ToXml(con,typeof(context)); Response.Write(str_output);
            Response.Write(str_output); 
                    
                

 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号