C# xmlSerializer簡單用法示例
本文實例講述了C# xmlSerializer簡單用法。分享給大家供大家參考,具體如下:
先上實體類
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>
PS:這里再為大家提供幾款比較實用的xml相關(guān)在線工具供大家使用:
在線XML格式化/壓縮工具:
http://tools.jb51.net/code/xmlformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《C#程序設計之線程使用技巧總結(jié)》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O計入門教程》
希望本文所述對大家C#程序設計有所幫助。
相關(guān)文章

C# 使用 GDI+ 實現(xiàn)添加中心旋轉(zhuǎn)(任意角度)的文字