存取类实例的参数最佳的方法当然是串行化技术,串行化支持两种方式:二进制方式,可以高保真的保存类示例,另一种是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();
- }
- }
- }
- //-----------------------------------
本文出自 “叶帆工作室” 博客,转载请与作者联系!