一个C#的序列化问题
我现在xml文件过大,好像序列化也报错,用了GC
/// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">对象</param>
/// <param name="fileName"></param>
/// <returns></returns>
public static void SerializeToFile<T>(T t, string fileName)
{
GC.Collect();
MemoryStream ms = new MemoryStream();
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.Encoding = new UTF8Encoding(false);
settings.Indent = true;
using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(ms, settings))
{
for (int a = 0; a < 10000; a++)
{
try
{
Thread.Sleep(500);
System.Xml.Serialization.XmlSerializer xz =
new System.Xml.Serialization.XmlSerializer(t.GetType());
xz.Serialize(xw, t);
}
catch (Exception e)
{
Console.WriteLine("数据太大,休息一下");
}
}
}
using (TextWriter tw = new StreamWriter(fileName))
{
tw.Write(Encoding.UTF8.GetString(ms.ToArray()));
tw.Close();
}
} xml c# --------------------编程问答-------------------- 例子:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
[Serializable]
public class Object5
{
public int i1 = 0;
public int i2 = 0;
public float f3=0;
public string str;
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(72, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Object5 obj = new Object5();
obj.i1 = 128;
obj.i2 = 24;
obj.f3=1.3f;
obj.str = "Some String";
double d1=1.3d;
float f1=1.3f;
int i1=1;
string s1="HelloWorld";
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.Stream stream = new System.IO.FileStream("File.bin", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
formatter.Serialize(stream, obj);
formatter.Serialize(stream,d1);
formatter.Serialize(stream,f1);
formatter.Serialize(stream,i1);
formatter.Serialize(stream,s1);
stream.Close();
formatter=null;
}
}
} --------------------编程问答-------------------- 不要用MemoryStream,直接用文件流往文件里写。 --------------------编程问答-------------------- 二楼的仁兄,你的例子能访着我的写一下吗 --------------------编程问答--------------------
有DEMO么 --------------------编程问答-------------------- 你怎么知道是文件过大,那个消息是你自己想的啊? --------------------编程问答-------------------- 无聊,居然把一个对象反复序列化10000次
补充:.NET技术 , C#