C# xmlSerializer簡(jiǎn)單用法示例
本文實(shí)例講述了C# xmlSerializer簡(jiǎn)單用法。分享給大家供大家參考,具體如下:
先上實(shí)體類
public class Entity { public Entity() { } public Entity(string c, string f) { name = c; school = f; } public string name; public string school; }
使用時(shí)聲明
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); }
對(duì)應(yīng)的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:這里再為大家提供幾款比較實(shí)用的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#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#動(dòng)態(tài)執(zhí)行批處理命令的方法
這篇文章主要介紹了C#動(dòng)態(tài)執(zhí)行批處理命令的方法,可實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行一系列控制臺(tái)命令,并允許實(shí)時(shí)顯示出來執(zhí)行結(jié)果,需要的朋友可以參考下2014-11-11C#迭代器模式(Iterator Pattern)實(shí)例教程
這篇文章主要介紹了C#迭代器模式(Iterator Pattern),包括了迭代器的適用范圍及用法實(shí)例,需要的朋友可以參考下2014-09-09

Silverlight實(shí)現(xiàn)跑馬燈動(dòng)畫

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