“ Matryoshka”类-读取和写入XML

问题描述 投票:0回答:1

[因此,最近我有机会使用一种技术,由于缺乏更好的用语,在俄罗斯嵌套娃娃之后,我将其称为“ Matroyshka类”。该类具有List属性,该属性包含相同类的实例,每个实例也具有相似的列表,或多或少具有任意的“深度”。]

这里是一个看起来像的简化示例:

class Doll
{
    public string Color;
    public List<Doll> ChildDolls;
    // End of properties. Getters and Setters not included for readability.

    public Doll(string color)
    {
        this.Color = color;
        this.ChildDolls = new List<Doll>();
    } // End of Constructor

    public void AddChild(Doll NewChild)
    {
        this.ChildDolls.Add(NewChild);
    } // End of Add Child method

    public override string ToString()
    {
        string output;

        // Adds the current doll's color
        output += this.Color + "\n";

        // Adds each doll's children, and each of theirs, and so on...
        foreach (Doll Child in this.ChildDolls)
        {
            output += Child.ToString();
        }
        return output;
    } // End of To String method

} // End of class

反正我碰壁了。我需要能够将它们读写到XML文件(或者我想是一些类似的外部文件)的能力,因为我的程序最终将涉及其中的lot;将它们放入代码本身似乎是不明智的。使用类似于示例的ToString()方法的技术,编写应该相对简单。但是,由于缺乏随意的“深度”,我缺乏在事后阅读它们的想法。

c# xml class xmlreader
1个回答
0
投票

System.Xml.Serialization名称空间中有您需要的一切。这是您的Matryoshka类的外观示例(仅需一些小改动):

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

public class Doll  //must be public
{
    public string Color { get; set; }
    public List<Doll> ChildDolls { get; set; } = new List<Doll>();

    public Doll() //needs a ctor without parameters
    {
        Color = "Not set";
    }

    public Doll(string color)
    {
        this.Color = color;
    } // End of Constructor

    public void AddChild(Doll NewChild)
    {
        this.ChildDolls.Add(NewChild);
    } // End of Add Child method

    public override string ToString()
    {
        var output = new StringBuilder();

        // Adds the current doll's color
        output.AppendLine(Color);

        // Adds each doll's children, and each of theirs, and so on...
        foreach (Doll Child in this.ChildDolls)
        {
            output.Append(Child.ToString());
        }
        return output.ToString();
    } // End of To String method

} // End of class

这是序列化/反序列化方法:

    public static void Serialize(string path, Doll matryoshka)
    {
        using (var writer = new StreamWriter(path))
        {
            var s = new XmlSerializer(typeof(Doll));
            s.Serialize(writer, matryoshka);
        }
    }

    public static Doll Deserialize(string path)
    {
        using (var reader = new StreamReader(path))
        {
            var x = new XmlSerializer(typeof(Doll));
            return (Doll)x.Deserialize(reader);
        }
    }

使用它们非常简单:

        var redDoll = new Doll("red");

        var green1Doll = new Doll("green1");
        var green2Doll = new Doll("green2");

        var blue1Doll = new Doll("blue1");
        var blue2Doll = new Doll("blue2");

        redDoll.AddChild(green1Doll);
        redDoll.AddChild(green2Doll);

        green1Doll.AddChild(blue1Doll);

        green2Doll.AddChild(blue2Doll);

        Serialize("dolls.xml", redDoll);

结果将是这样:

<?xml version="1.0" encoding="utf-8"?>
<Doll xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Color>red</Color>
  <ChildDolls>
    <Doll>
      <Color>green1</Color>
      <ChildDolls>
        <Doll>
          <Color>blue1</Color>
          <ChildDolls />
        </Doll>
      </ChildDolls>
    </Doll>
    <Doll>
      <Color>green2</Color>
      <ChildDolls>
        <Doll>
          <Color>blue2</Color>
          <ChildDolls />
        </Doll>
      </ChildDolls>
    </Doll>
  </ChildDolls>
</Doll>

反序列化也可以正常工作:

        var deserializedDoll = Deserialize("dolls.xml");
        Console.Write(deserializedDoll.ToString());

预期的输出是:

red
green1
blue1
green2
blue2

您还可以使用属性控制序列化程序如何生成XML。这是docs

© www.soinside.com 2019 - 2024. All rights reserved.