Client的数据保存在ArrayList中,比较复杂,因此不好组成文本类型的请问如何系列化,如何保存到数据库中。

解决方案 »

  1.   

    用分隔符把ArrayList的内容分开.当成字符串保存...获取的时候再分Split()拆分保存成ArrayList里面就可啦了...整个ArrayList保存,还真是闻所未闻...数据库无法识别它的类型...
      

  2.   

    存取类实例的参数最佳的方法当然是串行化技术,串行化支持两种方式:二进制方式,可以高保真的保存类示例,另一种是XML方式,它仅保存公共数据。很可惜.net 2.0的精简框架集仅支持XML方式。      我这里做了一个示例,实现的功能是在PC机上可以画很多图形,用串行化方式保存相关信息,把相关信息下载到wince中,由wince中的c#程序读取串行化信息,并把相关类的实例信息还原出来。     这里面有个关键,图形类有可能有多个(示例为2个),而目前我查相关资料,都是一个类的串行化存取,并且如果你存两个以上的类,用XML是可以存取成功的,但是读取的时候它会告诉你失败。所以这里引入了ArrayList类的相关概念。      也就是说,我定义了一个类,类中的一个属性为ArrayList类的实例,这样用ArrayList实例我可以存储很多的类信息。      同样,不作任何处理用一般方法存储是成功的,但是在读取时,你发现ArrayList实例中的数据,都是object类型,原类型信息丢失!      这怎么办?继续查资料,发现有两种方法可以解决这个问题。      1、  [XmlElement(Type = typeof(YFRect)), XmlElement(Type = typeof(YFCircle))]
                  public ArrayList gData = new ArrayList();          //图元数据              在类中添加XmlElement声明,把ArrayList 类实例中有可能添加的类都标示出。       2、在存取数据时,用代码告诉XML串行化相关类的类型              Type[] gt = new Type[2];   //图元类型数组
                gt[0] = typeof(YFRect);
                gt[1] = typeof(YFCircle);
                
                Stream sf = new FileStream(strXmlFile, FileMode.Open, FileAccess.Read, FileShare.None);
                XmlSerializer xmls = new XmlSerializer(typeof(YFGraphicsData), gt);
                XmlData = (YFGraphicsData)xmls.Deserialize(sf);
                sf.Close();      这是运行后的结果: 相关代码:clsGraphics.cs    (图元类)using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.Drawing;
    using System.Xml.Serialization;
    using System.IO;
    using System.Xml;namespace YFXMLSaveLoad
    {   
        //图元数据类
        public class YFGraphicsData
        {
            //[XmlElement(Type = typeof(YFRect)), XmlElement(Type = typeof(YFCircle))]
            //当代码传入类型数组时,则不需要上面的声明
            public string strName = "测试";
            public string strVer = "V1.0.0";
            public ArrayList gData = new ArrayList();          //图元数据
           
        }    //串行化操作类
        public class YFXMLSerialize
        {
            //串行化
            public void XMLSerializer(YFGraphicsData XmlData,string strXmlFile)
            {
                Type[] gt = new Type[2];  //图元类型数组
                gt[0] = typeof(YFRect);
                gt[1] = typeof(YFCircle);            Stream sf = new FileStream(strXmlFile, FileMode.Create, FileAccess.Write, FileShare.None);
                XmlSerializer xmls = new XmlSerializer(typeof(YFGraphicsData), gt);
                xmls.Serialize(sf, XmlData);
                sf.Close();
            }        //反串行化
            public void XMLDeserialize(out YFGraphicsData XmlData, string strXmlFile)
            {
                Type[] gt = new Type[2];   //图元类型数组
                gt[0] = typeof(YFRect);
                gt[1] = typeof(YFCircle);
                
                Stream sf = new FileStream(strXmlFile, FileMode.Open, FileAccess.Read, FileShare.None);
                XmlSerializer xmls = new XmlSerializer(typeof(YFGraphicsData), gt);
                XmlData = (YFGraphicsData)xmls.Deserialize(sf);
                sf.Close();
            }       
      
        }
        
        //------------------------------------------------
        public class YFGraphicsBase
        {     
            public int  width = 1;
            //Color类不支持XML串行化
            public int color = 0;   
            public virtual void Draw(Graphics e) { }
        }    public class YFRect : YFGraphicsBase
        {
            public Rectangle xy;
            public override void Draw(Graphics e)
            {
                e.DrawRectangle(new Pen(Color.FromArgb(color), width), xy);
            }
        }    public class YFCircle : YFGraphicsBase
        {
            public Rectangle xy;       
            public override void Draw(Graphics e)
            {
                e.DrawEllipse(new Pen(Color.FromArgb(color), width), xy);
            }
        }
        
    }Form1.cs 窗体代码using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Xml.Serialization;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;namespace YFXMLSaveLoad
    {
        public partial class Form1 : Form
        {        YFGraphicsData XmlData = new YFGraphicsData();    //图元数据
            YFXMLSerialize XmlWork = new YFXMLSerialize();    //XML串行化方法         public Form1()
            {
                InitializeComponent();
                panel1.Refresh(); 
            }        //自绘
            private void button4_Click(object sender, EventArgs e)
            {
                YFRect yfr001=new YFRect();
                YFRect  yfr002 = new YFRect();
                YFCircle yfc001 = new YFCircle();            yfr001.color = Color.Blue.ToArgb();  
                yfr001.xy.X  = 10;
                yfr001.xy.Y   = 10;
                yfr001.xy.Width  = 50;
                yfr001.xy.Height   = 50;            yfr002.color = Color.FromArgb(0, 0, 0).ToArgb(); 
                yfr002.width = 2;
                yfr002.xy.X = 30;
                yfr002.xy.Y = 50;
                yfr002.xy.Width = 100;
                yfr002.xy.Height = 80;            yfc001.color = Color.Red.ToArgb();
                yfc001.xy.X = 20;
                yfc001.xy.Y = 20;
                yfc001.xy.Width = 80;
                yfc001.xy.Height = 90;            XmlData.gData.Clear();
                XmlData.gData.Add(yfr001);
                XmlData.gData.Add(yfc001);
                XmlData.gData.Add(yfr002);            panel1.Refresh();                    }           //绘图
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.Clear(Color.PapayaWhip);
                foreach (YFGraphicsBase dw in XmlData.gData)
                {
                    dw.Draw(e.Graphics);               
                }
                textBox1.Text = XmlData.gData.Count.ToString();
            }        //清图元
            private void button3_Click(object sender, EventArgs e)
            {
                XmlData.gData.Clear();
                panel1.Refresh(); 
            }        //保存图元
            private void button2_Click(object sender, EventArgs e)
            {
                //图元串行化
                XmlWork.XMLSerializer(XmlData,"TuData.xml");
                //------ 
                MessageBox.Show("OK");  
            }        //调入图元
            private void button1_Click(object sender, EventArgs e)
            {
                //图元反串行化
                XmlWork.XMLDeserialize(out XmlData, "TuData.xml");
                //------  
                panel1.Refresh(); 
            }        }
    }//----------------------------------- 
      

  3.   

    http://www.cnblogs.com/niuniu502/archive/2007/02/13/649141.html
      

  4.   

    //将一个对象序列化成二进制数组
            public static byte[] SerializeToByte(System.Collections.ArrayList list)
            {
                try
                {
                    //假如Arraylist为空,则返回null
                    if (list.Count == 0)
                    {
                        return null;
                    }
                    byte[] array = null;
                    //定义一个流
                    System.IO.Stream stream = new System.IO.MemoryStream();
                    //定义一个格式化器
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    //将Arraylist中的对象逐一进行序列化
                    foreach (object obj in list)
                    {
                        bf.Serialize(stream, obj);
                    }
                    array = new byte[stream.Length];
                    //将二进制流写入数组
                    stream.Position = 0;
                    stream.Read(array, 0, (int)stream.Length);
                    //关闭流
                    stream.Close();                return array;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }        //将一个二进制数组反序列化
            public static System.Collections.ArrayList DeseralizeToArraylist(byte[] b)
            {
                try
                {
                    if (b == null || b.Length == 0)
                    {
                        return null;
                    }
                    //定义一个流
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(b);
                    //定义一个格式化器
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();                System.Collections.ArrayList list = new System.Collections.ArrayList();                while (stream.Position != stream.Length)
                    {
                        list.Add(bf.Deserialize(stream));
                    }
                    stream.Close();                return list;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
      

  5.   

    序列化与反序列化我只是用在Image处理上...觉得处理ArrayList还是保存成XML比较好...