Client的数据保存在ArrayList中,比较复杂,因此不好组成文本类型的请问如何系列化,如何保存到数据库中。
解决方案 »
- lock语句后的小括号的表达式到底是什么意思?
- C# 写入流的问题
- 我在网上找的LISTVIEW样式 他给出了一段代码 可是我不知道怎么使用这些代码
- winform 控制panel滚动条滑块的位置
- 如何把本机的.NET Framework 版本:2.0.50727变成1.1.4322.2300
- vs2010是否可以纯静态编译
- 我写一个桌面应用程序,要在每天指定的时间自动启动,执行完后自动关闭。请问如何实现自动启动和关闭,谢谢
- c#中有没有用于视频捕捉窗口建立的函数?
- 如何得到磁盘的类型以及磁盘卷标?
- 怎样在C#中编程建立SQL数据库和表?(无内容)
- 遇到麻烦了.
- 救命啊 .事务已被隐式或显式提交,或已终止
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();
} }
}//-----------------------------------
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;
}
}