ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 2311|回复: 5

[讨论] C#XML序列化练习

[复制链接]

TA的精华主题

TA的得分主题

发表于 2013-4-3 11:39 | 显示全部楼层 |阅读模式
案例来自
http://club.excelhome.net/forum. ... p;page=1#pid6817292

要求把附件的汇总工作表的数据生成XML文件。

主要的序列化子过程:
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             Scene[] sc = null;
  4.             subExcel myDemo = new subExcel("汇总",@"d:\temp\汇总.xlsm");
  5.             sc = myDemo.GetArr();

  6.             SerializaDemo(sc);
  7.             textBox1.Text = @"文件路径 d:\temp\汇总.xml";
  8.         }

  9.         //XML序列化主过程
  10.         void SerializaDemo(Scene[] sc)
  11.         {
  12.             StreamWriter sr = new StreamWriter(@"d:\temp\汇总.xml");
  13.             XmlSerializer mySerializer = new XmlSerializer(typeof(Scene[]), new XmlRootAttribute("NpcSceneUpdate"));
  14.             mySerializer.Serialize(sr, sc);
  15.             sr.Close();
  16.         }
复制代码
需序列化的主类模块:
  1. public class Scene
  2.     {
  3.         private Npc[] m_npcs = null;
  4.         private string mapId;

  5.         [XmlAttribute(AttributeName = "mapId")]
  6.         public string MapId
  7.         {
  8.             get { return mapId; }
  9.             set { mapId = value; }
  10.         }

  11.         [XmlElement(ElementName = "Npc")]
  12.         public Npc[] Npcs
  13.         {
  14.             get { return m_npcs; }
  15.             set { m_npcs = value; }
  16.         }
复制代码
主要子类模块
  1. public class Npc
  2.     {
  3.         private string typeId;
  4.         private string name;
  5.         private string npcFlag;
  6.         private string x;
  7.         private string y;
  8.         private string posRandRange;
  9.         private string direction;
  10.         private string num;
  11.         private string interactiveCount;
  12.         private string refreshInterval;
  13.         private string patrolRange;
  14.         private string isAggressive;
  15.         private string viewDistance;
  16.         private string bindMonsterGroup;
  17.         private string monsterGroups;

  18.         [XmlAttribute(AttributeName = "typeId")]
  19.         public string TyPeId
  20.         {
  21.             get { return typeId; }
  22.             set { typeId = value; }
  23.         }

  24.         [XmlAttribute(AttributeName = "name")]
  25.         public string Name
  26.         {
  27.             get { return name; }
  28.             set { name = value; }
  29.         }

  30.         [XmlAttribute(AttributeName = "npcFlag")]
  31.         public string NpcFlag
  32.         {
  33.             get {return npcFlag; }
  34.             set { npcFlag = value; }
  35.         }

  36.         [XmlAttribute(AttributeName = "x")]
  37.         public string X
  38.         {
  39.             get { return x; }
  40.             set { x = value; }
  41.         }

  42.         [XmlAttribute(AttributeName = "y")]
  43.         public string Y
  44.         {
  45.             get { return y; }
  46.             set { y = value; }
  47.         }

  48.         [XmlAttribute(AttributeName = "posRandRange")]
  49.         public string PosRandRange
  50.         {
  51.             get { return posRandRange; }
  52.             set { posRandRange = value; }
  53.         }

  54.         [XmlAttribute(AttributeName = "direction")]
  55.         public string Direction
  56.         {
  57.             get { return direction; }
  58.             set { direction = value; }
  59.         }

  60.         [XmlAttribute(AttributeName = "num")]
  61.         public string Num
  62.         {
  63.             get { return num; }
  64.             set { num = value; }
  65.         }

  66.         [XmlAttribute(AttributeName = "interactiveCount")]
  67.         public string InteractiveCount
  68.         {
  69.             get { return interactiveCount; }
  70.             set { interactiveCount = value; }
  71.         }

  72.         [XmlAttribute(AttributeName = "refreshInterval")]
  73.         public string RefreshInterval
  74.         {
  75.             get { return refreshInterval; }
  76.             set { refreshInterval = value; }
  77.         }

  78.         [XmlAttribute(AttributeName = "patrolRange")]
  79.         public string PatrolRange
  80.         {
  81.             get { return patrolRange; }
  82.             set { patrolRange = value; }
  83.         }

  84.         [XmlAttribute(AttributeName = "isAggressive")]
  85.         public string IsAggressive
  86.         {
  87.             get { return isAggressive; }
  88.             set { isAggressive = value; }
  89.         }

  90.         [XmlAttribute(AttributeName = "viewDistance")]
  91.         public string ViewDistance
  92.         {
  93.             get { return viewDistance; }
  94.             set { viewDistance = value; }
  95.         }

  96.         [XmlAttribute(AttributeName = "bindMonsterGroup")]
  97.         public string BindMonsterGroup
  98.         {
  99.             get { return bindMonsterGroup; }
  100.             set { bindMonsterGroup = value; }
  101.         }

  102.         [XmlAttribute(AttributeName = "monsterGroups")]
  103.         public string MonsterGroups
  104.         {
  105.             get { return monsterGroups; }
  106.             set { monsterGroups = value; }
  107.         }
  108.     }
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-4-3 11:46 | 显示全部楼层
源代码

附件测试环境:windows7:vs2010:.net4.0: office2010(64)

ExcelDemo.rar

60.58 KB, 下载次数: 22

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-4-3 11:48 | 显示全部楼层
生成的 XML文档

汇总.rar

1.48 KB, 下载次数: 13

TA的精华主题

TA的得分主题

发表于 2013-4-6 23:38 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
正找这方面资料,作记号学习

TA的精华主题

TA的得分主题

发表于 2014-10-14 15:44 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
马克,刚好用到!

TA的精华主题

TA的得分主题

发表于 2015-10-19 16:29 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-1 22:18 , Processed in 0.039141 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表