源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C# xmlSerializer简单用法示例

  • 时间:2021-03-27 19:33 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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>

[b]PS:这里再为大家提供几款比较实用的xml相关在线工具供大家使用:[/b] [b]在线XML格式化/压缩工具: [/b][url=http://tools.jb51.net/code/xmlformat]http://tools.jb51.net/code/xmlformat[/url] [b]在线XML/JSON互相转换工具: [/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url] [b]XML[/b][b]在线压缩/格式化工具: [/b][url=http://tools.jb51.net/code/xml_format_compress]http://tools.jb51.net/code/xml_format_compress[/url] [b]XML[/b][b]代码在线格式化美化工具: [/b][url=http://tools.jb51.net/code/xmlcodeformat]http://tools.jb51.net/code/xmlcodeformat[/url] 更多关于C#相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/266.htm]C#中XML文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/165.htm]C#常见控件用法教程[/url]》、《[url=http://www.1sucai.cn/Special/227.htm]C#程序设计之线程使用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/125.htm]WinForm控件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/116.htm]C#数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/265.htm]C#数组操作技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/478.htm]C#面向对象程序设计入门教程[/url]》 希望本文所述对大家C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部