Save a tree as XML using XmlSerializer

In one of my projects, the need to preserve such a tree structure:

image

As it happens, this project is the development of language is C #, then I think of XmlSerializer, from the Internet to find some tutorials,

Finally managed to successfully complete the task:

   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>

 

Code explain everything, XmlSerializer and XML Attribute convenient self-evident.

Reproduced in: https: //www.cnblogs.com/jalenwang/archive/2011/07/28/save_tree_as_xml.html

Guess you like

Origin blog.csdn.net/weixin_34357436/article/details/93414836