序列化和反序列化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.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
//System.Runtime.Serialization

namespace WebApplication2
{

    
public class SerializeObject
    
{
    
        
        
/// <summary>
        
/// Returns the set of included namespaces for the serializer.
        
/// </summary>
        
/// <returns>
        
/// The set of included namespaces for the serializer.
        
/// </returns>

        public static XmlSerializerNamespaces GetNamespaces()
        
{
            XmlSerializerNamespaces ns;
            ns 
= new XmlSerializerNamespaces();
            ns.Add(
"xs""http://www.w3.org/2001/XMLSchema");
            ns.Add(
"xsi""http://www.w3.org/2001/XMLSchema-instance");
            
return ns;
        }


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

        }


        

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


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


    }

}

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

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


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

    }

}



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

            Response.Write(
"Context1 = " + con.context1);
            Response.Write(
"Context2 = " + con.context2);
            Response.Write(
"Context3 = " + con.context3);
            Response.Write(
"Context4 = " + con.context4);
            Response.Write(
"Context5 = " + con.context5);

反序列化:
  string str_output=(string)SerializeObject.ToXml(con,typeof(context));
            Response.Write(str_output);


posted @ 2006-12-22 18:05  jhtchina  阅读(917)  评论(0)    收藏  举报