1. XML序列化实例演示
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace XML序列化实例
{
class Program
{
static void Main(string[] args)
{
var list = new List<User>();
var random = new Random();
for (int i = 0; i < 2; i++)
{
var id = random.Next(0, 10000);
var user = new User
{
ID = id,
Name = "Name" + id,
Age = random.Next(1, 100)
};
var dic = new SerializableDictionary<Guid, Guid>();
for (int j = 0; j < 3; j++)
{
dic.Add(Guid.NewGuid(), Guid.NewGuid());
}
user.Dictionary = dic;
list.Add(user);
}
Stopwatch sw = new Stopwatch();
//XmlSerializer
sw.Start();
var xmlSerializer = new XmlSerializer(typeof(List<User>));
//const string xmlfile = "xml.txt";
const string xmlfile = "user.xml";
var fi = new FileInfo(xmlfile);
using (var stream = fi.Create())
{
xmlSerializer.Serialize(stream, list);
}
sw.Stop();
fi.Refresh();
Console.WriteLine("XML Time : {0} , Size : {1}K", sw.Elapsed, fi.Length / 1024);
Console.Read();
}
}
}