• Save a tree as XML using XmlSerializer


    在我的一个项目中,需要保存一个这样的树结构:

    image

    恰好,这个项目的开发语言是C#,这时我就想到了XmlSerializer,从网上找了一些教程,

    最后总算圆满完成任务:

       1: [XmlRoot("HotLines")]
       2: public class HotLines
       3: {
       4:     #region Constructor...
       5:     public HotLines()
       6:     {
       7:         Lines = new List<HotLine>();
       8:         LineGroups = new List<HotLines>();
       9:     }
      10:     #endregion
      11:  
      12:     #region Properties...
      13:     [XmlAttribute("Name")]
      14:     public string Name
      15:     {
      16:         get;
      17:         set;
      18:     }
      19:  
      20:     [XmlArray("Lines")]
      21:     [XmlArrayItem("Line", typeof(HotLine))]
      22:     public List<HotLine> Lines
      23:     {
      24:         get;
      25:         set;
      26:     }
      27:  
      28:     [XmlArray("LineGroups")]
      29:     [XmlArrayItem("Lines", typeof(HotLines))]
      30:     public List<HotLines> LineGroups
      31:     {
      32:         get;
      33:         set;
      34:     }
      35:     #endregion
      36:  
      37:     #region Utilities...
      38:     static public void SerializeToXML(HotLines lines, string fileName)
      39:     {
      40:         XmlSerializer serializer = new XmlSerializer(typeof(KingTools.HotLines));
      41:         TextWriter textWriter = new StreamWriter(fileName);
      42:         serializer.Serialize(textWriter, lines);
      43:         textWriter.Close();
      44:     }
      45:  
      46:     static public HotLines DeserializeFromXML(string fileName)
      47:     {
      48:         XmlSerializer deserializer = new XmlSerializer(typeof(KingTools.HotLines), new Type[] { typeof(HotLine) });
      49:         TextReader textReader = new StreamReader(fileName);
      50:         var lines = (HotLines)deserializer.Deserialize(textReader);
      51:         textReader.Close();
      52:         return lines;
      53:     }
      54:     #endregion
      55: }
      56:  
      57: [XmlRoot("Line")]
      58: public class HotLine
      59: {
      60:     #region Properties...
      61:     [XmlElement("File")]
      62:     public string File
      63:     {
      64:         get;
      65:         set;
      66:     }
      67:  
      68:     [XmlElement("Column")]
      69:     public int Column
      70:     {
      71:         get;
      72:         set;
      73:     }
      74:  
      75:     [XmlElement("Comment")]
      76:     public string Comment
      77:     {
      78:         get;
      79:         set;
      80:     }
      81:     #endregion
      82: }

    看看最终的代码,不得不为C#开发效率的高效而折服。用C++保存这样的一份数据,想想都怕,不知道其中要出多少幺蛾子。

    下面是一份测试代码:

       1: HotLines root = new HotLines();
       2: root.Name = "Root";
       3:  
       4: HotLine aLine = new HotLine();
       5: aLine.Column = 10;
       6: aLine.File = "Test";
       7:  
       8: root.Lines.Add(aLine);
       9:  
      10: HotLines group = new HotLines();
      11: group.Name = "group";
      12:  
      13: root.LineGroups.Add(group);
      14: group.Lines.Add(aLine);
      15:  
      16: string fileName = @"c:\lines.xml";
      17: HotLines.SerializeToXML(root, fileName);
      18:  
      19: var newRoot = HotLines.DeserializeFromXML(fileName);
      20:  
      21: Assert.AreEqual(newRoot.Name, root.Name);
      22: Assert.AreEqual(newRoot.Lines.Count, root.Lines.Count);
      23: Assert.AreEqual(newRoot.LineGroups.Count, root.LineGroups.Count);

    测试代码生成的XML如下:

       1: <?xml version="1.0" encoding="utf-8"?>
       2: <HotLines xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="Root">
       3:   <Lines>
       4:     <Line>
       5:       <File>Test</File>
       6:       <Column>10</Column>
       7:     </Line>
       8:   </Lines>
       9:   <LineGroups>
      10:     <Lines Name="group">
      11:       <Lines>
      12:         <Line>
      13:           <File>Test</File>
      14:           <Column>10</Column>
      15:         </Line>
      16:       </Lines>
      17:       <LineGroups />
      18:     </Lines>
      19:   </LineGroups>
      20: </HotLines>

    代码说明一切,XmlSerializer 和 XML Attribute的便利不言自明。

  • 相关阅读:
    memset()函数,多用于清空数组
    Echart 词云图 上手代码 同含(echarts-wordcloud.js)最简单的教程 复制可用
    爬虫使用真实浏览器打开网页进行爬取
    jsoup 模拟登陆github网页(源代码)亲测可用 直接复制就能用
    拷贝虚拟电脑 Ubuntu 系统 含hadoop hive hbase mysql spark eclipse
    Python 连接MySQL 增删改查 直接可用(最简易,含源码)
    Python 中文词频统计,热词统计,简要分析(含上手源码)
    百度百科简介爬取(含源代码、信息领域词频数据csv格式)
    博客园博文爬取 标签爬取(含源代码)
    输入一行电报文字,将字母变成其下一字母
  • 原文地址:https://www.cnblogs.com/jalenwang/p/save_tree_as_xml.html
Copyright © 2020-2023  润新知