先上实体类
public class Entity
{
public Entity()
{
}
public Entity(string c, string f)
{
name = c;
school = f;
}
public string name;
public string school;
}使用时声明
List<Entity> entityList=null;
XmlSerializer xs = new XmlSerializer(typeof(List<Entity>));读入
using (StreamReader sr = new StreamReader(configPath))
{
entityList = xs.Deserialize(sr) as List<Entity>;
}输出
using (StreamWriter sw = File.CreateText(configPath))
{
xs.Serialize(sw, entityList);
}对应的xml
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entity>
<Name>Alice</Name>
<School>SJTU</School>
</Entity>
<Entity>
<Name>Cici</Name>
<School>CSU</School>
</Entity>
<Entity>
<Name>Zero</Name>
<School>HIT</School>
</Entity>
</ArrayOfEntity>
本文介绍了一个使用C#进行XML序列化的简单实例,包括如何定义实体类、读取和写入XML文件的过程。
883

被折叠的 条评论
为什么被折叠?



