我现在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();
}
}xmlc#
解决方案 »
- 关于,调用api,返回的json值
- list排序问题.
- .NET老师给我们留的一个很难的问题,VisualStdio表单中添加了一个button,然后双击它…………请教各位大牛
- textBox1.Text 为什么在按扭里面有效,在方法里面就无效了呢?这是什么为呢?
- SharpMap添加点问题
- Datagird 问题:如何实现点击其中一格的事件处理问题(吐血大耍分,就这么点了,可怜可怜小妹了)
- gridview rowcommand 如何获取当前行的值
- 问一个C# 语法的问题
- 如何在Windows Form的datagrid中,根据行中某一个字段的条件,设置行颜色?
- 如何截取字节长度。不是字符。
- c#绑架c++程序后,c++程序不响应鼠标左右键?
- 问题:设置按钮打开磁盘浏览器
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;
}
}
}