码迷,mamicode.com
首页 > Windows程序 > 详细

C# XML序列化实例演示

时间:2020-08-24 15:15:27      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:ons   name   eric   refresh   ++   star   XML   实例   size   

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();

 


}


}
}

C# XML序列化实例演示

标签:ons   name   eric   refresh   ++   star   XML   实例   size   

原文地址:https://www.cnblogs.com/kexiong/p/13553458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!